preg_replace() beginner

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamoo1980
Forum Newbie
Posts: 1
Joined: Fri Jul 28, 2006 3:23 am

preg_replace() beginner

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Post 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!
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Post by ronverdonk »

See link to that article viewtopic.php?t=33147

Ronald :mrgreen:[/url]
mahr
Forum Newbie
Posts: 11
Joined: Sat Aug 12, 2006 10:43 am

Post by mahr »

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);
?>
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]
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Re: preg_replace() beginner

Post 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
Last edited by christian_phpbeginner on Sun Aug 13, 2006 4:04 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Quick note, delimeters can be whatever you want them to be, as long as they match eachother.
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post 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 "#" ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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. :wink:
Post Reply