Jeffro Posted June 3, 2011 Share Posted June 3, 2011 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! Quote Link to comment Share on other sites More sharing options...
Zane Posted June 3, 2011 Share Posted June 3, 2011 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 Quote Link to comment Share on other sites More sharing options...
Jeffro Posted June 3, 2011 Author Share Posted June 3, 2011 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! Quote Link to comment Share on other sites More sharing options...
Zane Posted June 3, 2011 Share Posted June 3, 2011 That $1 stands for what is in the parentheses... everything after Sl There's a good tutorial on regular expression on the main site. http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax Quote Link to comment Share on other sites More sharing options...
Jeffro Posted June 3, 2011 Author Share Posted June 3, 2011 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. Quote Link to comment Share on other sites More sharing options...
.josh Posted June 3, 2011 Share Posted June 3, 2011 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. Quote Link to comment Share on other sites More sharing options...
Jeffro Posted June 3, 2011 Author Share Posted June 3, 2011 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.