PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Tue Mar 13, 2007 3:18 am
Hi
I'm trying to replace "-" with – in my HTML output from a DB using str_replace() but it doesn't seem to work - is there a better way or could anyone point out why it won't work for me?
Code: Select all
$text= str_replace( "-", "–", $text);
Many thanks
louie35
Forum Contributor
Posts: 144 Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:
Post
by louie35 » Tue Mar 13, 2007 3:28 am
that should work
Code: Select all
$text= str_replace( "-", "–", $text);
$text= str_replace( "-", "–", $text);
$text= str_replace( "-", "–", $text);
duplicate the code and check it out. Had a similar issue myself and by duplicating the code on few line worked.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Mar 13, 2007 3:28 am
Works fine for me
Code: Select all
<?php
$text = 'a-b-c-d-e';
$text= str_replace( "-", "–", $text);
echo php_sapi_name(), '. ', $text;
?>cli. a–b–c–d–e
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Tue Mar 13, 2007 3:44 am
Not sure what I'm doing wrong - can't get it to work for me.
This is (a slightly shortened version of) my code, is there anything that stands out as being particularly stupid? :
Code: Select all
include("includes/dbsetup.php");
$result = @mysql_query("SELECT id, company_name, address, town, county, postcode FROM members ORDER BY town ASC");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$company_name = $row['company_name'];
$address = $row['address'];
$town = $row['town'];
$county = $row['county'];
$postcode = $row['postcode'];
$company_name = htmlspecialchars($company_name);
$address = htmlspecialchars($address );
$town = htmlspecialchars($town );
$county = htmlspecialchars($county);
$postcode = htmlspecialchars($postcode );
$company_name= str_replace( "-", "–", $company_name);
$address= str_replace( "-", "–", $address);
$town= str_replace( "-", "–", $town);
$county= str_replace( "-", "–", $county);
$postcode= str_replace( "-", "–", $postcode);
echo '<dt>' . $town . ' – ' . $company_name . '</dt>' . '<dd>' . '<strong>' . 'Address: ' . '</strong>' . '<br />';
if ($address != '')
{
echo $address . '<br />' ;
} else {
echo ''; // don't show
}
if ($town != '')
{
echo $town . '<br />';
} else {
echo ''; // don't show
}
if ($county != '')
{
echo $county . ' ' ;
} else {
echo ''; // don't show
}
if ($postcode != '')
{
echo $postcode . '<br />';
} else {
echo ''; // don't show
}
echo '</dd>';
}
louie35
Forum Contributor
Posts: 144 Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:
Post
by louie35 » Tue Mar 13, 2007 4:12 am
yeah...
Code: Select all
//put this line
$company_name = htmlspecialchars($company_name);
//after this line
$company_name= str_replace( "-", "–", $company_name);
//like this
$company_name= str_replace( "-", "–", $company_name);
$company_name = htmlspecialchars($company_name);
Noobie
Forum Commoner
Posts: 85 Joined: Sun May 15, 2005 11:38 am
Post
by Noobie » Tue Mar 13, 2007 6:58 am
Thanks - I'll give it a go!
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Tue Mar 13, 2007 1:46 pm
Noobie wrote:
is there anything that stands out as being particularly stupid?
Perhaps this
echo ''; // don't show
In php, there's no need to explicitly write "echo nothing" to print just nothing.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Mar 13, 2007 2:22 pm
Make sure that the string you are looking for is actually there using
strpos() or
strstr() . You might be seeing it one way but it may be totally different.
Code: Select all
<?php
if (strpos($search_string, '-') !== false)
{
// It was in here so replace
}
?>