Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    get a part of string from a string

    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

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Does the string have those newline characters or were you just giving an example?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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)?

  • #4
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    I just giving a example.
    if the string have not those newline characters, how you can deal with that?

  • #5
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    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.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by kamkam View Post
    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:

    PHP Code:
    $myobject preg_replace("/^[^<]*<object(.+)<\/object>[^>]*$//","<object$1</object>",$page); 
    best regards
    Last edited by oesxyl; 03-01-2008 at 09:28 AM.

  • #7
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    Thanks a lot.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •