Welcome to WebmasterWorld Guest from 54.147.20.131

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

replacing string

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

     

whatson

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

WebmasterWorld Senior Member 10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month