Jump to content

How do I turn a Regex ghost variable into a solid variable?


TylerDiaz

Recommended Posts

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?

Link to comment
Share on other sites

$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

 

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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. ;)

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.