Jump to content

PHP strpos not working with Variable

Hello, trying to learn and use PHP.

I use a php script to gather data from a xml file. and add a ?Storename="Netcdf:" to the php call  ../php/script.php?Storename="Netcdf:"

$url = "http://localhost:8080/geoserver/wms?version=1.3.0&request=GetCapabilities&service=WMS";

$str = file_get_contents( $url );
$storename = (string)$_GET['Storename'];
$xml = simplexml_load_string( $str );


$returnArray = array();
//echo $storename;
foreach($xml->Capability->Layer->Layer as $child) {  	
	if (strlen(strpos($child->Name, "Netcdf:" , 0 )) > 0 ) { 
     //if (strlen(strpos($child->Name, $storename , 0 )) > 0 ) {  
        
          
        array_push($returnArray, array('name' => (string)$child->Name, 
                                       'title' => (string)$child->Title, 
                                       'dimension' => (string)$child->Dimension));
 	}

}


echo json_encode($returnArray);
echo $storename;

The Code is working with the needle-string hardcoded but not when given with a variable ( see // if statement)

I guess it has something to do with the return value not being 0 or 1 instead it is boolean.

gettype($storename) is string

echo $storename returns "Netcdf:"  but the returned Array is empty when using $storename

 

Does anybody has an idea as to why php not using my string as string?

Link to comment
https://linustechtips.com/topic/975614-php-strpos-not-working-with-variable/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×