SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    webality...dunno what it means bcp_2005's Avatar
    Join Date
    Jun 2002
    Location
    Huntsville Alabama
    Posts
    394
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace help

    Ok I want to use preg_replace() to get rid of everything in a phrase except a certain word. Like in the sentence "Hello big world" I wanted to erase everything except "world". How would I do this? Thanks...

  2. #2
    Resistance is Futile webgodjj's Avatar
    Join Date
    Nov 2002
    Location
    Madison, WI USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I'm not sure if this will help, however, try looking into
    str_replace

    This seems to be the function you are looking for, however, you might stil have the same question...

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you know exactly what word you want to keep:
    PHP Code:
    $string "Hello big world";
    $wanted "world";
    $this_is_the_word_you_want preg_replace("/.*?(".preg_quote($wanted).").*?/""\\\\1"$string); 
    But then again, if you already know exactly what word you want, you don't need to extract it (since you know what it is!). Perhaps you could explain a little more about what you're trying to achieve.

  4. #4
    webality...dunno what it means bcp_2005's Avatar
    Join Date
    Jun 2002
    Location
    Huntsville Alabama
    Posts
    394
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys! I found another way around my problem. Thanks for the help!

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by bcp_2005
    Thanks guys! I found another way around my problem. Thanks for the help!
    Well I'm curious.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    webality...dunno what it means bcp_2005's Avatar
    Join Date
    Jun 2002
    Location
    Huntsville Alabama
    Posts
    394
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I thought through my problem and I didn't need to erase all the other data afterall. So I just used ereg_replace() to locate a certain word and replace it with a different word. I guess my problem was that I just wasn't thinking through all my options.

  7. #7
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good enough for me... just curious.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  8. #8
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ereg_ functions are about four times slower then preg_ ones by the way.

  9. #9
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Codename49
    ereg_ functions are about four times slower then preg_ ones by the way.
    Heh, I was biting my tongue... I almost always point that out. But since you brought it up...

    Almost always, yes, as shown here:
    "preg is (approx. 4.7 times!) faster than eregi"
    (http://www.sitepointforums.com/showt...threadid=71134)
    Really not too much harder to write preg (Perl-style) regular expressions either, check out this quick tip: http://www.sitepointforums.com/showt...threadid=83886
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  10. #10
    webality...dunno what it means bcp_2005's Avatar
    Join Date
    Jun 2002
    Location
    Huntsville Alabama
    Posts
    394
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Codename49
    ereg_ functions are about four times slower then preg_ ones by the way.
    Thanks for the tip...


Bookmarks

Posting Permissions

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