...

View Full Version : get a part of string from a string



kamkam
03-01-2008, 02:57 AM
Hello;
I'd like to ask you how i can get a part of string out from a string by using regular expression, please.

i known how to do it by using substr(), but i think it may be have a better way than using substr().

I got a string as following:
$string="
this is part 1 of the string

<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/6ZRax870WxQ"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/6ZRax870WxQ" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>

this is a part 3 of a string";

and i want to get a part of string out from the above string, so the following result is what i want:

<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/6ZRax870WxQ"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/6ZRax870WxQ" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>


Thanks

_Aerospace_Eng_
03-01-2008, 03:04 AM
Does the string have those newline characters or were you just giving an example?

mlseim
03-01-2008, 05:05 AM
Give me an actual URL source of where you're trying to get the string from.
I will give you a PHP Curl method that is very easy. I'm assuming you're
scraping a website and picking out some portions (much like an RSS reader)?

kamkam
03-01-2008, 10:09 AM
I just giving a example.
if the string have not those newline characters, how you can deal with that?

kamkam
03-01-2008, 10:11 AM
Give me an actual URL source of where you're trying to get the string from.
I will give you a PHP Curl method that is very easy. I'm assuming you're
scraping a website and picking out some portions (much like an RSS reader)?

i just make a example for me easy to ask you a question.

oesxyl
03-01-2008, 10:23 AM
I just giving a example.
if the string have not those newline characters, how you can deal with that?

you must be more exact about what you want to extract to build the regex else the simple cases but useless are when the regex extract everything or nothing.
In this case, I don't test but could work something like this:



$myobject = preg_replace("/^[^<]*<object(.+)<\/object>[^>]*$//","<object$1</object>",$page);


best regards

kamkam
03-01-2008, 09:10 PM
Thanks a lot.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum