Skip to main content

Having issue with substr function in PHP



I have a string like this:







$geometry = "POINT (1.5041909054501184 0.39827301781943014)"







I have to split the two decimal values 1.5041909054501184 and 0.39827301781943014 based on space, into an array. For that, as expected, I have to chop-off 'POINT (' and ')' from the $geometry .





I tried the following lines:







$temp = substr($geometry , strpos($geometry, "(")+1, strlen($geometry)-2);







and







$temp = substr($geometry , strpos($geometry, "(")+1, strpos($geometry, ")")-1);







Echoing $temp in both the cases displays string as:







"1.5041909054501184 0.39827301781943014)"







How can I remove the ')' from the string $geometry ?





UPDATE





How can I generalize it to strings like these?







$geometry = "POINT (1.5041909054501184 0.39827301781943014)";







and







$geometry = "POLYGON ((1.5049088554391572 0.39805485932781448, 1.5049135685638309 0.39805660717232405, 1.5049147247575003 0.39805462248168044, 1.5049101547531727 0.39805287533491257, 1.5049088554391572 0.39805485932781448))";




Comments

  1. Try this:

    $str = 'POINT (1.5041909054501184 0.39827301781943014)';
    // Remove any character that is not digit, dot or space, then trim whitespace
    $stripped = trim(preg_replace('/[^0-9 \.]/', '', $str));
    // Split by the space in the middle
    $exploded = explode(' ', $stripped);

    print_r($exploded);
    /*
    Array
    (
    [0] => 1.5041909054501184
    [1] => 0.39827301781943014
    )
    */

    ReplyDelete
  2. I would just use a regular expression:

    $geometry = "POINT (1.5041909054501184 0.39827301781943014)";
    preg_match_all('/POINT \((\d\.\d+) (\d\.\d+)/ ', $geometry, $matches);

    $matches[1][0]; // => "1.1.5041909054501184"
    $matches[2][0]; // => "0.39827301781943014"


    Note that these results are strings, you'll have to cast them to floats to use them as floats.

    ReplyDelete
  3. Maybe better with preg_replace ?

    $geometry = "POINT (1.5041909054501184 0.39827301781943014)";

    echo preg_replace('/POINT \((.*?)\)/uis','$1',$geometry);

    ReplyDelete
  4. Well, why not just remove them?

    $geometry = "POINT (1.5041909054501184 0.39827301781943014)";
    $temp1 = str_replace("POINT (", "", $geometry);
    $temp = str_replace(")", "", $temp1);


    Assuming your geometry syntax is always the same, this will do just fine.

    Also, since you're starting 7 characters in advance, and you want to chop the last one, give the $length argument a value of length-8:

    $temp = substr($geometry , strpos($geometry, "(")+1, strlen($geometry)-8);


    Both of these echo correctly.



    Edit!

    Based on the comments:

    Try this one:

    $temp = preg_replace("|.+\((.+)\)|", "$1", $geometry);

    ReplyDelete
  5. The third argument to substr is not "end point" but "substring length".

    So you need to say:

    $start_pos = strpos($geometry, "(")+1;
    $temp = substr($geometry , $start_pos , strlen($geometry)-$start_pos);


    Then you can use explode to get the two decimals;

    list($x, $y) = explode(' ', $temp);

    or

    $coordArray = explode(' ', $temp);

    ReplyDelete
  6. The 3d parameter should be the length of the substring, not the position in the original string

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.