Can't find where this </br> is coming into a link.

neogrant

Junior Member
Jul 6, 2010
7
0
0
Can't find where this </br> is coming into a link.

PHP:
<?php
if(isset($_POST['submit'])) { //execute only if an item is posted.
$name = preg_replace('~([^a-zA-Z])~','',$_POST['name']) . "\n"; //strip out all but alpha characters in the posted name.
$file = fopen('names.txt','a'); //open file('names.txt') or create if it doesn't exist.
fwrite($file,"<a href='http://xwis.net/ra2/players/$name'>".$name."</a>"); //write the name to the end of the file.
fclose($file); //close the file.
}
echo implode('<br />',file('names.txt'));  //print the contents to the page, one name per line.
?>
<form action="" method="post">
<label>Name: </label><br />
<input type="text" name="name" value="" /><br />
<input type="submit" name="submit" value=" Submit " />
</form>
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,694
4,657
75
Don't see a /b, but the ' . "\n"' at the end of line 3 looks a little weird. You really want a newline at the end of every $name, before "</a>"? Perhaps you want it after the "</a>"?

Edit: Is there a reason a simple '[^a-zA-Z]' doesn't work in the preg_replace?
 
Last edited:

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Not sure what you are asking really, but from your rather cryptic question I can only guess that the value of $name isn't what you are expecting at some point. Try printing the value of $name before and after you run your regex on it to see where the problem might be.
 

troytime

Golden Member
Jan 3, 2006
1,996
1
0
you're pulling the text file in as an array. every line of the txt file will be an array element.

the first few lines of your txt:
<a href='http://xwis.net/ra2/players/test
'/>test
</a><a href='http://xwis.net/ra2/players/wqewqe
'/>wqewqe
</a><a href='http://xwis.net/ra2/players/neo
'/>neo
</a><a href='http://xwis.net/ra2/players/neo
/'>neo
You then implode each element with a <br />, which is producing malformed html.
<a href='http://xwis.net/whatever<br />'/>neo<br /></a>

1. looks like the code you pasted isn't the exact code on your site.
2. why are you adding the closing slash in your <a> tags? isn't the </a> after the anchor text enough?
 

CoinOperatedBoy

Golden Member
Dec 11, 2008
1,809
0
76
As suggested, do not concatenate the name with '\n'. You'll want the newline at the actual end of the line in your output document. Also, since you're already using double quotes to evaluate $name, I don't think you need to do a string concatenation with the . operator. See:

PHP:
$name = preg_replace('~([^a-zA-Z])~','',$_POST['name']); //strip out all but alpha characters in the posted name.
PHP:
fwrite($file,"<a href='http://xwis.net/ra2/players/$name'>$name</a>\n");
 
Last edited: