Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex problem

Status
Not open for further replies.

Zoom1234

Programmer
Joined
Oct 30, 2003
Messages
116
Location
BE
Hello,

I have to convert
$str = '<PARAM NAME=movie VALUE="intro.swf">' ;// double quotes surrounding value may or may not be there
to
$str = '<PARAM NAME=movie VALUE="d:\intro.swf">' ;

Any help is welcome. kinda urgent basis :)

Thanks

 
$str=str_replace("intro.swf","d:\intro.swf",$str);

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks for the reply Karver!!

However the name of the value paramter ie . intro.swf in this case is dynamic.
Actually i am reading a file which is uplaoded on the server and want to replace the path of the swf file used.

so i guess i have to use preg_replace() or function which supports REGEX.

Any ideas ?
 
try
Code:
preg_replace("/(\w).swf/i", "d:\\\${1}.swf", $your_string_here);
 
DaZZleD:

I tried it but its still not working :(
 
cheat entirely - I trust the path is all that needs appending:

$str = '<PARAM NAME=movie VALUE="d:\intro.swf">' ;

so
$path='d:\';

$str = '<PARAM NAME=movie VALUE="'.$path.'intro.swf">' ;

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
woops used the wrong first $str but you see the idea.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks for the replies

Karver:
cheat entirely - I trust the path is all that needs appending:


For that i need to know where to append the $path ie the exact position.

 
ok back up a little, .. ok all the way to the beginning....

$str=str_replace('VALUE="','VALUE="d:\',$str);

tryig my hardest to keep this away form the likes of preg or ereg replace, for simplicity.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
$str1='<PARAM NAME=movie VALUE="intro.swf">';
$Path="ASD";
str1=preg_replace("/value="(.*)"/i","value=\""+$Path+"$1\"")
echo $str1;

Known is handfull, Unknown is worldfull
 
oops, replace the + with . (in $path)

Known is handfull, Unknown is worldfull
 
vbkris
Sorry, but your regex won't work:
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit]) - the subject in your example was omitted. Also, what's a bit more tricky is that Zoom1234 said that
double quotes surrounding value may or may not be there
;)

Zoom1234
Here's a solution suing preg_match to extract the parts you need and manipulate them. It looks like regex chinese, but is pretty simple:
Code:
<?php
$str1='<PARAM NAME=movie VALUE="intro.swf">';
$Path="ASD";
preg_match('/(<param name=movie value=["]*)([^">]*)(["]*.*)/i',$str1,$myarr);
$newStr = $myarr[1].$Path.$myarr[2].$myarr[3];
echo($newStr);
?>
The expression explained:
/ open regex pattern definition
( open first sub pattern
< literal: tag open
param name=movie value= literal text to be matched
["]* a double quote or none
) close first sub pattern
( open second sub pattern
[ character class definition open
^ not the following characters
"> chars to be excluded, double quote and tag close
]* close char class and match any number of times
) close second sub pattern
( open third subpattern
["]* once again a char class that only contains the double quote, match 0 or more instances
.* whatever there is to the end of the string
) close third sub pattern
/ close pattern definition
i switch to do case insensitive matching

There would be some finer points to explain, like .* can have different meanins according to switches, but that would really exceed the scope of this explanation.
 
Good monday morning guys :))

I will try it and get back to you

Thanks a lot

 
hi DRJ,
i dont have PHP in my system, so i wrote the RegEx in javascript and did some copy paste into php.

preg_replace("/value="?(.*)"?/i","value=\""+$Path+"$1\"")

oughtnt this work???


Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top