SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why does this code not work?

    The following is a test snippet piece of code from a script. This snippet is supposed to split a list of urls from the urls' description and remove the urls scheme (http, https) if present. Here is the code:
    Code:
    <?php
    ///test urls NOTE "|" separates url form the urls description
    $inputuri = <<<URLS
    http://testa.com|description1
    http://testa.com|description1
    http://testb.com|description2
    testc.com|description3
    URLS;
    
    echo "<pre>".$inputuri ."</pre><br />";
    
    $inputuri_exp = explode("\r", $inputuri);//explodes inputuri for multipe urls
    
    
    foreach($inputuri_exp as $value)
    	{
    		$value_exp = explode("|", $value);//explodes value to get url and description
    
    		$url = strtolower($value_exp[0]);//url value
    
    		$descr = $value_exp[1];//url description
    
    		$url_parsed = parse_url($url);//url parsed to get scheme
    
    		$scheme = $url_parsed['scheme'];//set scheme to variable
    		
    
    		//removes url scheme from $urls variable if present
    		if(stripos($url, $scheme) === FALSE)
    			{
    				$url = $url;//does not contain a scheme like http or https
    
    			}else
    			{
    				$url = substr($url, strlen($scheme) + 3);//contains a scheme adds 3 characters for ://
    			
    			}
    		echo $url. "::". $descr. "<br />";
    		
    	}
    
    ?>
    I expect the output to be:
    Code:
    http://testa.com|description1
    http://testa.com|description1
    http://testb.com|description2
    testc.com|description3
    
    
    testa.com::description1
    testa.com::description1
    testb.com::description2
    testc.com::description3
    But instead it is what is below. Notice how the second "testa.com" and "testb.com" still have their schemes' (http://)? It seems after going through the foreach loop once the scheme is not found in the url even if present. I have been racking my brain for hours. What have I done wrong?

    Code:
    http://testa.com|description1
    http://testa.com|description1
    http://testb.com|description2
    testc.com|description3
    
    
    testa.com::description1
    http://testa.com::description1
    http://testb.com::description2
    testc.com::description3

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Heres a fix that works on win32, you needed to explode on "\n", so that might behave differently depending on how your text editor hanles line ends.

    \r\n
    \n
    etc

    also, stop nasty warnings by checking whether a 'scheme' has been found or not.

    Overall though, I discovered all this by:

    1 Using var_dump() on the first operation, explode().
    2 Running with display_errors = 1

    If you lost a day (and a few mill braincells) struggling with this, learn those 2 lessons real fast and lets get some work done...

    BTW, thanks for posting such a well-thought out question, this was dead easy for me to chase down on your behalf.
    sample code
    what I have
    what I want

    I wish everyone would do the same.

    PHP Code:
    <?php
    ini_set
    ('display_errors');
    error_reporting(E_ALL);


    ///test urls NOTE "|" separates url form the urls description
    $inputuri = <<<URLS
    http://testa.com|description1
    http://testa.com|description1
    http://testb.com|description2
    testc.com|description3
    URLS;

    echo 
    "<pre>".$inputuri ."</pre><br />";

    $inputuri_exp explode("\n"$inputuri);//explodes inputuri for multipe urls

    var_dump($inputuri_exp);  
    // above is the debug line 
    // the resulting output shows explode failed

    foreach($inputuri_exp as $value)
        {
            
    $value_exp explode("|"$value);//explodes value to get url and description

            
    $url strtolower($value_exp[0]);//url value

            
    $descr $value_exp[1];//url description

            
    $url_parsed parse_url($url);//url parsed to get scheme

    // put this condition in to stop warnings

    if( isset ($url_parsed['scheme'] ) )
            
    $scheme $url_parsed['scheme'];//set scheme to variable


            //removes url scheme from $urls variable if present
            
    if(stripos($url$scheme) === FALSE)
                {
                    
    $url $url;//does not contain a scheme like http or https

                
    }else
                {
                    
    $url substr($urlstrlen($scheme) + 3);//contains a scheme adds 3 characters for ://

                
    }
            echo 
    $url"::"$descr"<br />";

        }

    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help. I was not a where that of issued with different new line characters being used. Also, I was ignorant of the erros checking code to output errors to the browser.

    I decided to use preg_split instead of explode. That way the code is more robust since I can use the pattern match "/\r|\n/" in preg_split to search for both possibilities. Here is my altered code:

    Code:
    <?php
    ini_set('display_errors', 1 );
    error_reporting(E_ALL);
    
    
    ///test urls NOTE "|" separates url form the urls description
    $inputuri = <<<URLS
    http://testa.com|description1
    http://testa.com|description1
    http://testb.com|description2
    testc.com|description3
    URLS;
    
    echo "<pre>".$inputuri ."</pre><br />";
    
    //$inputuri_exp = explode("\r", $inputuri);//explodes inputuri for multipe urls
    
    $inputuri_exp = preg_split("/\r|\n/", $inputuri, -1, PREG_SPLIT_NO_EMPTY);
    
    var_dump($inputuri_exp);
    echo '<br />';
     
    // above is the debug line 
    // the resulting output shows explode failed
    
    foreach($inputuri_exp as $value)
    	{
    		$value_exp = explode("|", $value);//explodes value to get url and description
    
    		$url = strtolower($value_exp[0]);//url value
    
    		$descr = $value_exp[1];//url description
    
    		$url_parsed = parse_url($url);//url parsed to get scheme
    
    		
    		
    		
    		if(isset($url_parsed['scheme']))
    			$scheme = $url_parsed['scheme'];//set scheme to variable
    
    		//removes url scheme from $urls variable if present
    		if(stripos($url, $scheme) === FALSE)
    			{
    				$url = $url;//does not contain a scheme like http or https
    
    			}else
    			{
    				$url = substr($url, strlen($scheme) + 3);//contains a scheme
    			
    			}
    		echo $url. "::". $descr. "<br />";
    		
    	}
    
    ?>
    Quote Originally Posted by Cups View Post
    Heres a fix that works on win32, you needed to explode on "\n", so that might behave differently depending on how your text editor hanles line ends.

    \r\n
    \n
    etc

    also, stop nasty warnings by checking whether a 'scheme' has been found or not.

    Overall though, I discovered all this by:

    1 Using var_dump() on the first operation, explode().
    2 Running with display_errors = 1

    If you lost a day (and a few mill braincells) struggling with this, learn those 2 lessons real fast and lets get some work done...

    BTW, thanks for posting such a well-thought out question, this was dead easy for me to chase down on your behalf.
    sample code
    what I have
    what I want

    I wish everyone would do the same.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Thank you for your help. I was not a where that of issued with different new line characters being used.
    Thats OK. This is one of many times where using var_dump() carefully can reveal what PHP is actually doing on your behalf.

    As you get to rely on this more and more, you might find it worthwhile to set a variable at the top of your script, which you have to remember to turn off when you ftp the script to your server.
    PHP Code:
    <?php
    //$debug = false ;
    $debug true ;

    if( 
    $debug ){
    ini_set() // set error reporting etc

    }

    ... 
    later in your script ...

    if(
    $debug)
      
    var_dump($_POST) ;

    ... 
    more script etc
    Do you get the picture? Work like this for a while 'till you become more confident about they type of typical errors you find yourself making (we all made them!). Forgetting ; on line ends, missing a } etc.

    This is especially true when dealing with comparison operators, var_dump() will tell you when PHP thinks it is comparing a string '0' with an integer 0, for example.


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
  •