Jump to content

SOS: reg_replace syntax question


sageman

Recommended Posts

I want to use the URI as the page title. it currently reads as:

word-word-word.html

I want it to read as:

word word word

 

How can I modify the code in red to achieve these results for the page title?

 

$filename = $_SERVER['REQUEST_URI'];

$filename1 = split( '[/]', $filename );

$last = end($filename1);   

$last = ereg_replace("\.$", "", $last);

$underscore_key = $last;

$index_key = eregi_replace("(\.)([a-z]{2,4})$", '', $last);

$index_key = ereg_replace("\-", " ", $last);

$index_key = ereg_replace(".html", "", $last);

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/79710-sos-reg_replace-syntax-question/
Share on other sites

I'd use the preg suite. In general, it's much better than ereg. I would modify the whole section of code work something like this:

$filename = 'word1-word75-elephant.html';

$filename = preg_replace('/\..+$/', '', $filename); //chop off the extension
$filenames = preg_split('/-/', $filename); //split the words into an array
$filename = join(' ', $filenames); //rejoin the array with spaces
print_r($filename);

 

Or I suppose you could cut out the middle-man and use:

$filename = 'word1-word75-elephant.html';

$filename = preg_replace('/\..+$/', '', $filename);
$filename = preg_replace('/-/', ' ', $filename);
print_r($filename);

Thanks for your help so far...almost there.

I replaced $filename with $index_key because of my template.

 

The code below works great except that it returns

/word word word

instead of

word word word

 

How can I remove the / at the beginning

 

$index_key = $_SERVER['REQUEST_URI'];

$index_key = preg_replace('/\..+$/', '', $index_key);

$index_key = preg_replace('/-/', ' ', $index_key);

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.