Forum Moderators: coopster

Message Too Old, No Replies

PHP - Trying to use a function to replace with a dash

Can't get this code to work!

         

chopin2256

4:07 am on Jul 27, 2005 (gmt 0)

10+ Year Member



I don't know anything about php, but luckily enough, there is only one page of code. I stripped the main code, becuase I don't think its necessary to understand what I am trying to do. However if needed, I will add it. Basically, this code is a mod to convert the Invision Board urls into html. One annoying thing about this mod is that they use the separator _ which I want to convert to a dash obviously for better SEO purposes. Here is code, and I think this is what needs to be edited.

-----------code--------------------------------

function make_text_safe($text){
//Charachters must be in ASCII and certain ones aint allowed
$text = preg_replace("#&\#([0-9]*);#ie","_",$text);
$text = html_entity_decode ($text);
$text = str_replace(" / ","_",$text);
$text = str_replace("/","_",$text);
$text = str_replace("'","_",$text);
$text = str_replace(" - ","_",$text);
$text = str_replace("-","_",$text);
$text = str_replace(" ","_",$text);
$text = str_replace( "ä", "ae", $text);
$text = str_replace( "ö", "oe", $text);
$text = str_replace( "ü", "ue", $text);
$text = str_replace( "Ä", "Ae", $text);
$text = str_replace( "Ö", "Oe", $text);
$text = str_replace( "Ü", "Ue", $text);
$text = str_replace( "ß", "ss", $text);
$text = str_replace( "&", "and", $text);
$text = str_replace( "%", "Percent", $text);
//Polish Characters
$text = str_replace( "?", "l", $text);
$text = str_replace( "ó", "o", $text);
$text = str_replace( "?", "n", $text);
$text = str_replace( "?", "c", $text);
$text = str_replace( "?", "z", $text);
$text = str_replace( "?", "z", $text);
$text = str_replace( "?", "s", $text);
$text = str_replace( "?", "a", $text);
$text = str_replace( "?", "e", $text);
$text = ereg_replace("[^A-Za-z0-9_]", "", $text);
$text = str_replace("____","_",$text);
$text = str_replace("___","_",$text);
$text = str_replace("__","_",$text);
return $text;

--------------end of code------------------------

Obviously I tried this:

$text = str_replace(" ","-",$text); (I replaced the second pair of quotes containing the _ with a dash. This did not work. It merged everything into one long string. For some reason, only numbers, letters, and the stupid underscore work. The dash, period, and comma do not work.

Along with this file is just a shorter htaccess file, but I don't think the htaccess has anything to do with this.

Can someone please help me out?

ChadSEO

3:28 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



I believe the problem is with the line:

$text = ereg_replace("[^A-Za-z0-9_]", "", $text);

Essentially this says, remove anything that is not a letter, number, or underscore. You can add a dash to the line, like so:

$text = ereg_replace("[^A-Za-z0-9_-]", "", $text);

And this should fix it.

chopin2256

5:43 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



It worked! Thank you so much.