TylerDiaz Posted May 23, 2009 Share Posted May 23, 2009 Title asks the main question. Its something I have experimented with for hours now and cant seem to nail the coffin. First off here is the working script: <?php $data = "title:Example; of my work"; function fildercode($data) { $data = preg_replace("(title:(.+?)\;)is",'$1', $data); return $data; } echo filtercode($data); ?> Now, see this line? :: $data = preg_replace("(title:(.+?)\;)is",'$1', $data); How do I save that "$1" into a solid variable like: $solid = $1; echo "$solid"; See when I intent to echo or print the regex variable I get an error. So how can I grab the string of "Example" only? Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/ Share on other sites More sharing options...
RussellReal Posted May 23, 2009 Share Posted May 23, 2009 $1 references the first backreference, $2 will reference the nested backreference Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840934 Share on other sites More sharing options...
TylerDiaz Posted May 23, 2009 Author Share Posted May 23, 2009 $1 references the first backreference, $2 will reference the nested backreference I appreciate the theory explination. But if its not too much trouble may you please provide some sample code for me to base of and learn? Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840937 Share on other sites More sharing options...
RussellReal Posted May 23, 2009 Share Posted May 23, 2009 $data = preg_replace("(title:(.+?)\;)is",'$1', $data); preg_replace's arguements are pattern, replacement, input now, $N is used to reference the back references, which are captured thru capturing groups.. so your regex "/(title:(.+?)\;)/is" will match in the FIRST backreference(capturing group) (title:.... so it will capture everything that comes up inside the ( ), then you nested another capturing grou inside the first capturing group, which then sets whatever gets captured into the NEXT backreference up which would be the SECOND so in your current example, you'd want to use $2 in the replacement, not $1, which would return the first reference, which is everything + the second reference (in your example, its hard to explain) $data = preg_replace("/title:(.+?)\;/is",'$1', $data); use that instead, that would be better Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840943 Share on other sites More sharing options...
TylerDiaz Posted May 24, 2009 Author Share Posted May 24, 2009 $data = preg_replace("(title:(.+?)\;)is",'$1', $data); preg_replace's arguements are pattern, replacement, input now, $N is used to reference the back references, which are captured thru capturing groups.. so your regex "/(title:(.+?)\;)/is" will match in the FIRST backreference(capturing group) (title:.... so it will capture everything that comes up inside the ( ), then you nested another capturing grou inside the first capturing group, which then sets whatever gets captured into the NEXT backreference up which would be the SECOND so in your current example, you'd want to use $2 in the replacement, not $1, which would return the first reference, which is everything + the second reference (in your example, its hard to explain) $data = preg_replace("/title:(.+?)\;/is",'$1', $data); use that instead, that would be better Thanks, updated done. But now how do I store the regex to a stable variable? A variable that I could use across the entire php script. Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840965 Share on other sites More sharing options...
RussellReal Posted May 24, 2009 Share Posted May 24, 2009 not to be rude, but it'd probably be wise for you to learn php.. instead of ask 40 questions to get the script working the way you want it to, I know asking questions is healthy, but php basics, almost every php programmer's first code is a 'hello world' code, or something rly basic, unless they skipped ahead (like me) and aimed for something and skimmed php.net every 10 seconds.. and in the hello world, you echo 'hello world!';, in which case you'd probably understand what a string is, and what echo does, and like a lesson later or next step up would be learning variables, since PHP is ALL ABOUT variables.. like every other language.. so you'd know that substituting 'echo ' for '$whatever = ' in this statement: 'echo filtercode($data);' would capture the return of filtercode(..) and store it in $whatever, now, I know I sound very mean.. but taking the 30 minutes to do some tutorials doesn't ever hurt. Sorry again, Russell Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840967 Share on other sites More sharing options...
Ken2k7 Posted May 24, 2009 Share Posted May 24, 2009 Use preg_match_all followed by str_replace. The first one to capture all the matching patterns. The second to replace them. I'm not sure you can do it in one preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840978 Share on other sites More sharing options...
RussellReal Posted May 24, 2009 Share Posted May 24, 2009 ken, dude, preg_replace, he used preg_replace to replace the entire string with the back reference, but yes, preg_match would ofcourse be the better option, but unless I'm missing the big point, this should have been enough to make it work. Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840986 Share on other sites More sharing options...
TylerDiaz Posted May 24, 2009 Author Share Posted May 24, 2009 not to be rude, but it'd probably be wise for you to learn php.. instead of ask 40 questions to get the script working the way you want it to, I know asking questions is healthy, but php basics, almost every php programmer's first code is a 'hello world' code, or something rly basic, unless they skipped ahead (like me) and aimed for something and skimmed php.net every 10 seconds.. and in the hello world, you echo 'hello world!';, in which case you'd probably understand what a string is, and what echo does, and like a lesson later or next step up would be learning variables, since PHP is ALL ABOUT variables.. like every other language.. so you'd know that substituting 'echo ' for '$whatever = ' in this statement: 'echo filtercode($data);' would capture the return of filtercode(..) and store it in $whatever, now, I know I sound very mean.. but taking the 30 minutes to do some tutorials doesn't ever hurt. Sorry again, Russell Hmm not a bright awnser. I do know php - and have studied it an awful lot. So yes its insulting to proclaim someone with that statement. I ask questions out of curiosity - And I asked nicely for example code so I can learn and base of this solution along for anyone else with this complication. The theory of php is what complicates me, as I am self taught. Please refrain from stating unknowledged comments next time. Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840993 Share on other sites More sharing options...
RussellReal Posted May 24, 2009 Share Posted May 24, 2009 sigh.. dude, you asked how to contain the returned value in a variable.. lol Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840998 Share on other sites More sharing options...
TylerDiaz Posted May 24, 2009 Author Share Posted May 24, 2009 $1 references the first backreference, $2 will reference the nested backreference I appreciate the theory explination. But if its not too much trouble may you please provide some sample code for me to base of and learn? I think I remember asking nicely for some sample code as well. Quote Link to comment https://forums.phpfreaks.com/topic/159415-how-do-i-turn-a-regex-ghost-variable-into-a-solid-variable/#findComment-840999 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.