Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 03-03-2007, 02:59 AM   #1 (permalink)
Larry's Avatar
 

Join Date: May 2003

Posts: 1,797

Larry has disabled reputation

Default regular expression ??

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
Larry is offline  
Old 03-04-2007, 08:36 PM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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);
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?
__________________
Vormund is offline  
Old 03-04-2007, 11:22 PM   #3 (permalink)
Larry's Avatar
 

Join Date: May 2003

Posts: 1,797

Larry has disabled reputation

Default

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
Larry is offline  
Old 03-05-2007, 01:21 AM   #4 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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...!
__________________
Vormund is offline  
Old 03-06-2007, 01:38 AM   #5 (permalink)
Larry's Avatar
 

Join Date: May 2003

Posts: 1,797

Larry has disabled reputation

Default

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!!!!
Larry is offline  
Old 03-06-2007, 01:58 AM   #6 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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

I think the above regex will work -

PHP Code:
<?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."
"
;
$result preg_replace('/(\\$_mls_=\\")[^"]*+(\\")/''\\1' $myNewValue '\\2'$myFile );
echo 
"


"
;
foreach( 
$result as $lineNumber => $line )
    echo 
$line."
"
;
?>



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



blahargy="http://www.google.com";
$_mls_="Something New";
moreStuff="";
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!)
__________________
Vormund is offline  
Old 03-06-2007, 02:32 AM   #7 (permalink)
Larry's Avatar
 

Join Date: May 2003

Posts: 1,797

Larry has disabled reputation

Default

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!!!!!!!!!!!!
Larry is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On