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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex