SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a PHP equivalent of ASP replace() method?

    That says it all in the subject line ...

    in ASP

    Code:
    myNewString = replace(myOldString,"_"," ")
    Would replace all instances of "_" found in myOldString with " ".

    Can this also be done in PHP?

    PS - I do have a PHP book, just not a very good one, and I am a total beginner with PHP (very stuck in my ASP/VBscript ways, but I want to change!)
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think str_replace() should do the trick
    PHP Code:
    $text 'This_is_a_test.';
    $text str_replace("_"" "$text); 
    // $text is now: This is a test. 
    HTH

    -Helge
    Last edited by Helge; Nov 9, 2003 at 08:15. Reason: Typo in php function call. Wrote str_repalce instead of str_replace

  3. #3
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, join the dark side!

    PHP has functions for almost anything you can imagine.
    no more body = replace(body, vbCrLf, "<br />"),
    just a simple sweet $body = nl2br($body);

  4. #4
    Posts rarely lloydi's Avatar
    Join Date
    Jan 2002
    Location
    Swindon UK
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks - that seems to be working just fins. Now that I'm online and able to do more digging, I can see how much of a beginner's book I bought - hardly a thorough reference ...
    Build Your Own Web Site the Right Way!
    A beginners' HTML/CSS book with web standards at its heart
    The Ultimate HTML Reference
    A complete reference, in glorious hardback

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You best friend is the PHP manual.
    Another cool feature of php.net: If you know the functions name and want to get to it's manual page just go to http://php.net/my_function_name.
    The power of mod_rewrite, eh?

  6. #6
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by platinum
    ah, join the dark side!

    PHP has functions for almost anything you can imagine.
    no more body = replace(body, vbCrLf, "<br />"),
    just a simple sweet $body = nl2br($body);
    nl2br isn't str_replace("\n", '<br />', $body)
    It's str_replace("\n", "<br />\n", $body)
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  7. #7
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daijoubu
    nl2br isn't str_replace("\n", '<br />', $body)
    It's str_replace("\n", "<br />\n", $body)
    close enough


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
  •