[regular expression ??] - Computers



Search Tech-Forums - link takes you to our Forum's search page.

Note: The following is only a text archive!


To view the actual forum discussion, please visit our website at http://www.tech-forums.net

Pages:1



regular expression ??

(Click here to view the original thread with full colors/images)



Posted by: Larry

hello,

I can't figure this out!!! Its driving me crazy!!!


in a file I have this:

...
...
$_mls_="";
...
...


So, I need to replace (ie. set the variable to my value)
and I have this:

$result = preg_replace("_mls_=\"(.*)\"/", $repl_string, $line);

it keeps telling me: unknown modifier '='

anyone know how I can make the function FIND IT????

thanks



Posted by: Vormund

Regular expressions ALL the way!! I can complete most programming assignments in school with just a few regex's, of which they frown upon... it's so sad!

Anyways, one that would match exactly what you have there, would be:

[code]$result = preg_replace('/(\\$_mls_=\\")[^"]*+(\\")/', '\\1' . $repl_string . 'My Replacement Stuff\\2', $line);[/code]

Which is doing this:

To match:
1. Match $_mls_=" and capture that into back-reference #1
2. Match anything that is NOT a " unlimited times
3. Match " and capture that into back-reference #2

To replace:
1. '\\1' uses back-reference #1.
2. $repl_string is your new text.
3. '\\2' uses back-reference #2.

Is that what you wanted?



Posted by: Larry

uhm... wow.... sounds good.

this is more details on what I'm trying to do:

I have text (its actually an URL). Lets call it NEWURL.

I need to put that NEWURL as the value for $_mls_.

NOW: the existing value could be blank (ie. "")
or have a url for the value. (ie. "http://www.blahbalhbalh.com/blahblah/blahblah")

so in my example:
$line = a string of characters what contains: $_mls_="value"
$repl_string = the NEWURL


I would think ther are different ways to do it.. but I just need one :)

thanks



Posted by: Vormund

So basically, you have your original line that's like this:

$line = 'http://abcIdont/know/whats/here&_mls_=""&imAgoodPerson';

And then want to insert a value, _mls_="<here>", regardless if it is "" or "IlikePoodles"?

Sorry if I'm not getting this...!



Posted by: Larry

almost....
:)


I have text file (destination) that has this in it:
$_mls_="";


I need to add a value to the mls variable, ie. I need to replace the "" with "NEWURL"


I get the NEWURL from another text file (source). This source file is already processed by my code and I get he NEWURL value from it. Now I need to put that NEWURL inside the quotes in the destination file.

so I use the preg_replace() function to find the $_mls_ variable and need to write the NEWURL inside the quotes.

note: it could be that $_mls_ already has a url in the quote --- so I need to replace that url with NEWURL.

does that help?

THANKS for helping me!!!!



Posted by: Vormund

Of course! If I am actually helping...I love regular expressions:)

I think the above regex will work -

[php]<?php
$myNewValue = "Something New";
$myFile = file( "myFile.txt" );
/*
myFile.txt Contains:

blahargy="http://www.google.com";
$_mls_="hiThere";
moreStuff="";
*/
foreach( $myFile as $lineNumber => $line )
echo $line."<br>";
$result = preg_replace('/(\\$_mls_=\\")[^"]*+(\\")/', '\\1' . $myNewValue . '\\2', $myFile );
echo "<br><br><br>";
foreach( $result as $lineNumber => $line )
echo $line."<br>";
?>[/php]




This will output:
[quote]blahargy="http://www.google.com";
$_mls_="hiThere";
moreStuff="";



blahargy="http://www.google.com";
$_mls_="Something New";
moreStuff="";[/quote]
The first being before the regex, and the second after...works regardless if it's empty or not!

And that regex is matching anything starting with $_mls_=", up to the second ". That could be replaced with a linefeed/carriage return by using [^\r\n]*+ instead, if there might be quotes in it.

(And that is the real output, I just tried it...:) If I'm still missing something, let me know, as... I fear I am!)



Posted by: Larry

Vormund......

AWESOME!!!!!
it worked first try!!!!!!

sweet!!!!!!!!!!!!!!!!
I love you man!!!!!!

:)

really, I've been stuck on that thing for long time... i don't get regular expressions.... I know they are very powerful, usefull ---- never understood them

:)

many thanks!!!!!!!!!!!!





vBulletin Copyright ©2000 - 2003, Jelsoft Enterprises Limited.


PPC Management
vB Easy Archive Final - Created by Xenon