preg_replace() beginner
Moderator: General Moderators
preg_replace() beginner
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
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
- ronverdonk
- Forum Commoner
- Posts: 34
- Joined: Sat Jun 10, 2006 7:06 am
- Location: Netherlands
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!
Gives most of the important info you need, all in 1 page!
- ronverdonk
- Forum Commoner
- Posts: 34
- Joined: Sat Jun 10, 2006 7:06 am
- Location: Netherlands
feyd | Please use
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
,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);
?>Many thanks for the solution!
feyd | Please use
Code: Select all
,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]- christian_phpbeginner
- Forum Contributor
- Posts: 136
- Joined: Sat Jun 03, 2006 2:43 pm
- Location: Java
Re: preg_replace() beginner
You can find all the conventions of the preg_ functions in PHP at: http://www.php.net/manual/en/ref.pcre.php.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...
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: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!
Code: Select all
$pattern = "/[?iA-Z0-9]+\@[a-z]+\.\S/"; // the regex pattern are formatted or treated as STRING.Ahaa....you might have typed in this example:jamoo1980 wrote:Basically, I keep getting errors along the lines of "no ending delimiter ' found" etc.
Code: Select all
$pattern = "[?iA-Z0-9]+\@[a-z]+\.\S";Code: Select all
$pattern = "/[?iA-Z0-9]+\@[a-z]+\.\S/";cao,
Chris
Last edited by christian_phpbeginner on Sun Aug 13, 2006 4:04 am, edited 1 time in total.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- christian_phpbeginner
- Forum Contributor
- Posts: 136
- Joined: Sat Jun 03, 2006 2:43 pm
- Location: Java
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Read the first paragraph on the PHP Manual page on PCRE. The only characters not allowed are alphanumerics and the backslash '\'.

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