Jump to content

Easy preg_replace question


Jeffro

Recommended Posts

Easy for you... not me.  :)  (I still don't quite get preg_replace)

 

How do I code this? 

 

If $description start with Sl.

then replace the entire line with an empty space?

 

To make it a bit more clear what I'm asking...  If wildcards were such a thing, it would be like so: 

$description = str_replace('Sl.*','',$description)

 

Since there's no wildcard, I'm assuming preg_replace is the solution here? 

 

Thanks!

 

 

Link to comment
Share on other sites

Easy for you... not me.  :)  (I still don't quite get preg_replace)

 

How do I code this? 

 

If $description start with Sl.

then replace the entire line with an empty space?

 

To make it a bit more clear what I'm asking...  If wildcards were such a thing, it would be like so: 

$description = str_replace('Sl.*','',$description)

 

Since there's no wildcard, I'm assuming preg_replace is the solution here? 

 

Thanks!

 

 

Actually, that's really close to the syntax you would use for preg_replace's search pattern

$description = preg_replace("#^Sl(.*)#", "$1", $description);

That $1 stands for what is in the parentheses...  everything after Sl

Link to comment
Share on other sites

Easy for you... not me.  :)  (I still don't quite get preg_replace)

 

How do I code this? 

 

If $description start with Sl.

then replace the entire line with an empty space?

 

To make it a bit more clear what I'm asking...  If wildcards were such a thing, it would be like so: 

$description = str_replace('Sl.*','',$description)

 

Since there's no wildcard, I'm assuming preg_replace is the solution here? 

 

Thanks!

 

 

Actually, that's really close to the syntax you would use for preg_replace's search pattern

$description = preg_replace("#^Sl(.*)#", "$1", $description);

That $1 stands for what is in the parentheses...  everything after Sl

 

oh wow... so you can use a wildcard?  That's what .* is, correct? 

 

Also.. what is the $1 doing?  Thanks! 

Link to comment
Share on other sites

Nice tutorial.  Much better than others I've been searching out today.  But.. still doesn't answer my $1 question.  All it says it

These are just some example patterns. When I get into advanced syntax, you'll be able to create much more intricate patterns, such as:
/(\d)(\d{3})(?!\d)/
replacement: $1, $2

 

Still not understanding what $1 is doing.. since I never see it being created before being used.

 

 

Link to comment
Share on other sites

Well for your purposes, you don't actually need $1, as you are wanting to match and remove.  So you would really be doing:

 

$description = preg_replace("#^Sl.*#", "", $description);

 

Breakdown:

 

# beginning pattern delimiter.  preg_xxx functions require you to wrap the pattern in a delimiter.  You can use pretty much any non-alphanumeric character as the delimiter. Most people use / ~ or #

 

^ start of string anchor.  Tells the regex engine to start at the beginning of the string

 

SI look for and match the literal characters S and I

 

.* the dot says to "match any one character" (which matches almost anything, but I don't wanna complicate things for you).  The * says match 0 or more of any of the previous thing, so together, it says to match 0 or more of anything

 

# ending pattern delimiter.

 

 

-------

 

As to what $n (like $1) means...basically they are temporary variables the preg_replace (and some other functions) uses to store captured groups within the pattern.  For instance, the pattern Zanus originally supplied:

 

$description = preg_replace("#^Sl(.*)#", "$1", $description);

 

You see in there (.*)  the parenthesis mean to capture that part of the pattern.  That stuff will go into $1.  If you have more than one set of (...) in your pattern, $n represents the captured groups, in the order that they appear in the pattern. But for your purposes, since you are looking to replace the entire matched stuff with nothing, you don't need to capture anything or use $1 or anything.

 

Link to comment
Share on other sites

Well for your purposes, you don't actually need $1, as you are wanting to match and remove.  So you would really be doing:

 

$description = preg_replace("#^Sl.*#", "", $description);

 

Breakdown:

 

# beginning pattern delimiter.  preg_xxx functions require you to wrap the pattern in a delimiter.  You can use pretty much any non-alphanumeric character as the delimiter. Most people use / ~ or #

 

^ start of string anchor.  Tells the regex engine to start at the beginning of the string

 

SI look for and match the literal characters S and I

 

.* the dot says to "match any one character" (which matches almost anything, but I don't wanna complicate things for you).  The * says match 0 or more of any of the previous thing, so together, it says to match 0 or more of anything

 

# ending pattern delimiter.

 

 

-------

 

As to what $n (like $1) means...basically they are temporary variables the preg_replace (and some other functions) uses to store captured groups within the pattern.  For instance, the pattern Zanus originally supplied:

 

$description = preg_replace("#^Sl(.*)#", "$1", $description);

 

You see in there (.*)  the parenthesis mean to capture that part of the pattern.  That stuff will go into $1.  If you have more than one set of (...) in your pattern, $n represents the captured groups, in the order that they appear in the pattern. But for your purposes, since you are looking to replace the entire matched stuff with nothing, you don't need to capture anything or use $1 or anything.

 

Superb.  Better than any tut I found today.  Thank you! 

 

Glad you confirmed my $1 findings too.  I couldn't get his code to work until removing that and then voila.... 

 

Thanks again!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.