replace - with –

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

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

replace - with –

Post by Noobie »

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
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Works fine for me

Code: Select all

<?php
$text = 'a-b-c-d-e';
$text= str_replace( "-", "&ndash;", $text);

echo php_sapi_name(), '. ', $text;
?>
cli. a&ndash;b&ndash;c&ndash;d&ndash;e
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

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( "-", "&ndash;", $company_name);
				$address= str_replace( "-", "&ndash;", $address); 
				$town= str_replace( "-", "&ndash;", $town); 
				$county= str_replace( "-", "&ndash;", $county); 
				$postcode= str_replace( "-", "&ndash;", $postcode); 
				

	echo    '<dt>' .   $town . ' &ndash; ' . $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>';
	   }
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

yeah...

Code: Select all

//put this line

$company_name = htmlspecialchars($company_name);  

//after this line

$company_name= str_replace( "-", "&ndash;", $company_name); 

//like this

$company_name= str_replace( "-", "&ndash;", $company_name);
$company_name = htmlspecialchars($company_name);
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thanks - I'll give it a go!
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

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.
;)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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
}
?>
Post Reply