Archived Boards and Threads... > Parham's PHP Tutorials

PHP Lesson 17 - Regular Expressions

<< < (3/4) > >>

Parham:
I enjoyed writing this one a lot :)

Søren Bjerg:

--- Quote from: Parham on December 06, 2003, 11:52:42 PM ---[...] http://www.weitz.de/regex-coach/ [...]

--- End quote ---

Gotta say "The Regex Coach" is pretty helpful for regex newbies like myself, but I seem to have a problem with getting something, which works wonders in "The Regex Coach", to work in PHP;

Would like to remove the MD5 hash from the string below:


--- Code: ---$page = file.php?s=843157a0b0c79d147e7bb94c3a3e9f76&action=view
--- End code ---

The following highlights flawlessly in "The Regex Coach"...


--- Code: ---/?s=\w+
--- End code ---

...and I'd then use it like this


--- Code: ---preg_replace('/?s=\w+', 's=', $page);
--- End code ---

But the problem is the starting and ending with '/'s... can't seem to get it to work then :(.

Any straightforward solution I've totally gone blind from?

Tyris:
preg_replace('~/?s=\w+~', 's=', $page);
I'm still pretty useless with preg_replace stuff,... but that should work...
basically you need an opening and closing character (in this case "~") in regex statements.
the coach doesnt need them tho (as it deals with the statements themselves).

also... you're probably want
$page = preg_replace('~/?s=\w+~', 's=', $page);
(maybe...)

Søren Bjerg:
Ah... yes, that works as planned!Much thanks... I tried opening and ending with '/' as Parham explained in his first post, but I think I sort of get it now... appreciate your help :).

Tyris:
opening and ending with '/'  is fine... howver then you HAVE to escape any '/' characters within the regex... otherwise it thinks its the end... so using ~ is less of a problem coz they're far less used...
:)
glad I could help :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version