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
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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Solved...i think. ;D

thanks a lot for all your help

 

$index_key = $_SERVER['REQUEST_URI'];

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

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

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.