Page 1 of 1
preg_replace() beginner
Posted: Fri Jul 28, 2006 3:30 am
by jamoo1980
Hi all
I'm having a bit of trouble trying to understand the conventions for the preg_ functions in PHP, and also finding it difficult to find a reference on this topic...
I don't have any problem understanding what the function does, what arguments it takes, and what the results are supposed to be... The problem I have is in understanding how the strings are supposed to be formatted, and what all the symbols mean!
Basically, I keep getting errors along the lines of "no ending delimiter ' found" etc.
Can somebody recommend a web reference for the string formatting conventions?
Many thanks!
James
Posted: Fri Jul 28, 2006 8:47 am
by feyd
We have several stickies in our own Regex forum here, on this server. You may also look into explainations linked to from the Useful Posts thread.
Posted: Fri Jul 28, 2006 9:31 am
by ronverdonk
You can find a FREE Regular expression cheat sheet at
http://www.RegExLib.com
Gives most of the important info you need, all in 1 page!
Posted: Fri Jul 28, 2006 4:41 pm
by julian_lp
please read Regex section in this forum, I was totally lost with regex till I read it. Someone who I dont remember right now, explains regex expressions step by step. I've never found anything as useful as that
Posted: Fri Jul 28, 2006 5:52 pm
by ronverdonk
See link to that article
viewtopic.php?t=33147
Ronald

[/url]
Posted: Sat Aug 12, 2006 10:48 am
by mahr
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I just wondering if is possible to replace with preg_replace also hexa values of character and what is syntax for doing this. Is there any other way to do this job.
Example:
Code: Select all
<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>
As in this example I would like to have array and replace 5 (strange and non visible) characters with their XX XX hexa value in UTF-8 format.
Many thanks for the solution!
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Re: preg_replace() beginner
Posted: Sun Aug 13, 2006 2:07 am
by christian_phpbeginner
jamoo1980 wrote:Hi all
I'm having a bit of trouble trying to understand the conventions for the preg_ functions in PHP, and also finding it difficult to find a reference on this topic...
You can find all the conventions of the preg_ functions in PHP at:
http://www.php.net/manual/en/ref.pcre.php.
jamoo1980 wrote:I don't have any problem understanding what the function does, what arguments it takes, and what the results are supposed to be... The problem I have is in understanding how the strings are supposed to be formatted, and what all the symbols mean!
The strings or the patterns ? If you mean with: "How the patterns are supporsed to be formatted/treated ?". Well, they should be treated/formatted as well as a string then you pass it to the preg_ functions. Example:
Code: Select all
$pattern = "/[?iA-Z0-9]+\@[a-z]+\.\S/"; // the regex pattern are formatted or treated as STRING.
jamoo1980 wrote:Basically, I keep getting errors along the lines of "no ending delimiter ' found" etc.
Ahaa....you might have typed in this example:
Code: Select all
$pattern = "[?iA-Z0-9]+\@[a-z]+\.\S";
instead:
Code: Select all
$pattern = "/[?iA-Z0-9]+\@[a-z]+\.\S/";
Pay attention, the patterns are treated as a string, but the pattern itself has starting and ending demiliters, which is the forward-slash sign in this case.
cao,
Chris
Posted: Sun Aug 13, 2006 3:35 am
by RobertGonzalez
Quick note, delimeters can be whatever you want them to be, as long as they match eachother.
Posted: Sun Aug 13, 2006 4:04 am
by christian_phpbeginner
Everah wrote:Quick note, delimeters can be whatever you want them to be, as long as they match eachother.
Hi Everah, I thought it could only one either "/" or "#" ?
Posted: Sun Aug 13, 2006 10:31 am
by RobertGonzalez
Read the first paragraph on
the PHP Manual page on PCRE. The only characters not allowed are alphanumerics and the backslash '\'.
The PHP Manual wrote:The syntax for patterns used in these functions closely resembles Perl. The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters. See Pattern Syntax for detailed explanation.
PS I didn't know this for a long time. Thanks to this board and the PHP Manual, I are smartery now than I never was before.
