If X contains Y... what's the opperator for contains?

Discussion in 'PHP' started by vtechno42, Aug 10, 2006.

  1. #1
    I know it has to be waaay super easy, but I can't find the opperator for "contains" in the docu via php.net. Searched the forum, no luck.

    if($_SERVER['REQUEST_URI'] -contains- "blahblah.html")
    	{
    		$spit_it_out = "blahblah2.html";
    	}
    Code (php):

     
    vtechno42, Aug 10, 2006 IP
  2. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #2
    I don't wuite understand the question, but this might work:
    
    $ref=@$HTTP_REFERER;
    Switch($ref){
    	case "http://www.yoursiteurl.com/music":
    		header("Location: http://www.yoursiteurl.com/music/sent.html");
    		exit;
    		break;	
    	case "http://www.yoursiteurl.com/movies":
    		header("Location: http://www.yoursiteurl.com/movies/sent.html");
    		exit;
    		break;
    	case "http://www.yoursiteurl.com/cars":
    		header("Location: http://www.yoursiteurl.com/cars/sent.html");
    		exit;
    		break;
    }
    
    Code (markup):
    another way of doing it would be to put a hidden input with the URL as its value, and when the user submits the form you could just check the value of that field and redirect to the appropriate page.
     
    danielbruzual, Aug 10, 2006 IP
  3. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
          if(eregi("blahblah.html",$_SERVER['REQUEST_URI']))
    
              {
     
                  $spit_it_out = "blahblah2.html";
     
              } 
    PHP:
     
    sandossu, Aug 10, 2006 IP
  4. vtechno42

    vtechno42 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I edited the OP to clarify the question a little better. I don't want to use full URLs, basically all I need is the refering page, not the path.

    I'll explore that, it's probably a lot easier/more stable.
     
    vtechno42, Aug 10, 2006 IP
  5. rosytoes

    rosytoes Peon

    Messages:
    230
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    try the strstr and strpos functions
     
    rosytoes, Aug 10, 2006 IP
  6. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #6
    wouldn`t it be easier with eregi() function ?
     
    sandossu, Aug 11, 2006 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Simplest indeed is strpos, which will return false if it's not in there.

    I use this wrapper function for this:

    
    <?php
    function str_contains($haystack, $needle, $ignoreCase = false) {
       if ($ignoreCase) {
           $haystack = strtolower($haystack);
           $needle  = strtolower($needle);
       }
       $needlePos = strpos($haystack, $needle);
       return ($needlePos === false ? false : ($needlePos+1));
    }
    ?>
    
    PHP:
    That way, in your example, it's literally:

    
    if(str_contains($_SERVER['REQUEST_URI'], "blahblah.html") {
    $spit_it_out = "blahblah2.html";
    } 
    PHP:
     
    T0PS3O, Aug 11, 2006 IP