954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

regex

anyone can help me to make regular expression

[code ]

$string="
xxxxx
xxxxx

";

i want to remove all word from that string if that string have minimal three word'x'

preg_replace(); how to make that regex?

forzadraco
Junior Poster in Training
70 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 
i want to remove all word from that string if that string have minimal three word'x'


I am not clear what you want. Do you want to search for xxx, if found, remove it ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

yes remove all if get same pattern.. the problem is how to make regex pattern...

forzadraco
Junior Poster in Training
70 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 
$pattern = "/x{3}/";
$string = preg_replace($pattern,'', $string);

This will look for xxx, if found, replaces it with null.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

not like that.. iwant to remove all from if match with the pattern..

the pattern from

forzadraco
Junior Poster in Training
70 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

If you want to see if there are 3 or more x's, use:

$pattern = "/x{3,}/"


The {3} means looking for 3 of the preceding character but {3,} means 3or more. There's a great tutorial on regex in PHP here: http://www.sitepoint.com/article/regular-expressions-php/

bastones
Newbie Poster
7 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

This isn't the best solution (Infact, its kinda sloppy!). First, use preg_match to search for pattern "xxx". If found, then, use preg_match again, but this time, search for pattern, '/. This will again return the string between

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You