homepage Welcome to WebmasterWorld Guest from 54.196.195.158
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
replacing string
I know very basic, but cant remember how to do it
whatson




msg:4459801
 9:00 am on May 31, 2012 (gmt 0)

I want to change e.g. Some Sample Text to
some-sample-text
I know strtolower will make it lower case, but how do I replace the " " with -

 

g1smd




msg:4459806
 9:07 am on May 31, 2012 (gmt 0)

Use preg_replace.

See PHP manual for syntax.

For punctuation, I assume you'd either want to omit it or change it to hyphens.

$thisProductSlug = preg_replace('/[^a-z0-9]+$/', '', (preg_replace('/[^a-z0-9-]+/', '-', (preg_replace('/[\']+/', '', (preg_replace('/\ ?&\ ?/', '-', strtolower(trim($this->productName)))))))));

[edited by: g1smd at 9:16 am (utc) on May 31, 2012]

whatson




msg:4459807
 9:09 am on May 31, 2012 (gmt 0)

Ok sorry, worked it out
if (empty($string)) {
$string= strtolower(str_replace(' ','-',$string)); }

But what if the string contained other characters other than 0-9 and a-z, like ' or /, how can I strip them out?

rocknbil




msg:4459961
 4:19 pm on May 31, 2012 (gmt 0)

The previous preg does what you ask: "anything not a digit, letter, or dash . . . " (and other tidbits)

I want to change e.g. Some Sample Text to
some-sample-text


I know this is not a direct answer, but if this is to change titles into URL's, I implore you, don't do this. :-) You can see by the complicated regex and your reply that it's bereft with all sorts of problems, especially if it's to be managed by someone who doesn't know the complications of setting the title with special characters. Secondly your page URL is forever bound to the title, and often you want to change the title without affecting the URL. There's just too many ways it can go sideways.

Take the bite now and create a field just for the URL and call it a day. In any case, a / should always be disallowed in a page's URL because this indicates a directory, which is rarely (never?) what you want.

g1smd




msg:4459975
 4:31 pm on May 31, 2012 (gmt 0)

Yes. There's caveats with the code snippet I supplied. On the site where that code is used, the URL consists of a unique ID followed by the product slug. When a page is requested, the request is rewritten to the page generation script using only the product ID. The product ID is checked in the database, and 404 returned if there is no match. If the ID is valid, the product slug is checked. If it is not an exact match for this ID a redirect to the correct URL is sent. If it is a correct match the page content is served. If the title is ever altered, the URL changes, but the system is self correcting, automatically creating redirects for all incorrect requests.

It is better if the database has a separate field for URL rather than using the page name. In that case, I'd still use the system above to normalise the URL to lower case with hyphens.

whatson




msg:4460207
 1:29 am on Jun 1, 2012 (gmt 0)

I have an optional url field, but if it is left blank then it automatically uses the page name (not title).

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved