Page 1 of 1

replace - with –

Posted: Tue Mar 13, 2007 3:18 am
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

Posted: Tue Mar 13, 2007 3:28 am
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.

Posted: Tue Mar 13, 2007 3:28 am
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

Posted: Tue Mar 13, 2007 3:44 am
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>';
	   }

Posted: Tue Mar 13, 2007 4:12 am
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);

Posted: Tue Mar 13, 2007 6:58 am
by Noobie
Thanks - I'll give it a go!

Posted: Tue Mar 13, 2007 1:46 pm
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.
;)

Posted: Tue Mar 13, 2007 2:22 pm
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
}
?>