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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...