Monday, January 30, 2012

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))";

6 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