SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    Somewhere in the Middle East
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if NOT statement

    Hi everyone at site point,

    I'm trying to run the preg_replace function on a string but only if the variable $animal DOESN'T = "sheep". The problem is that the conditional statement I'm using (see below) doesn't run the function at all, even if the variable $animal equals something other than "sheep".

    if(!$animal=="sheep") {$formattedtext = preg_replace("/sheep/", "<a href='http://www.mysite.com/index.php?animal=sheep' class='littlelinks'>sheep</a>", $formattedtext, 1);}

    Any ideas?

    Many, many thanks, Leao

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if($animal != "sheep") {}

    Also, there is no need to use regular expressions if you are matching a literal string.
    Use str_replace instead.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2004
    Location
    Somewhere in the Middle East
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks mr cranial-bore. Problem solved.

    I'm using the preg_replace function because I want to replace only the first instance of the word "sheep". str_replace doesn't give me the option to do this. Is the preg_replace function really so slow? Doesn't seem so to me.

    Leao


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
  •