Jump to content

strings


DaveEverFade

Recommended Posts

This should be a nice easy one for you lot. I need a function that can get data from a string between 2 points. For example:

 

If I wanted the data between BEGIN and END in the following string:

 

0123 BEGIN456789END 0123 BEGIN10111213END

 

Ideally what would be returned would be an array of

 

$data[0]=456789

$data[1]=10111213

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/61367-strings/
Share on other sites

Your question is worded kinda funny but I am assuming you want to grab the data between BEGIN and END and put it into an array?

 

 

You can use the substr(); function to do this.

 

<?php

$var=BEGIN09840918END;

$out=substr($var, 5, -3);

echo $out;

?>

 

This should get you the result you are looking for.

Link to comment
https://forums.phpfreaks.com/topic/61367-strings/#findComment-305398
Share on other sites

This is crazy, but while you don't get any answer from a higher-skilled user you can stick with this code:

<?php
$string = "0123 BEGIN456789END 0123 BEGIN10111213END";
$search1 = strpos($string, "BEGIN");
$search2 = strpos($string, "END");
$s = $search2-$search1-5;
$final = substr($string, ($search1+5),$s);
echo $final."<br>";

$string = "0123 BEGIN456789END 0123 BEGIN10111213END";
$search1 = strrpos($string, "BEGIN");
$search2 = strrpos($string, "END");
$s = $search2-$search1-5;
$final = substr($string, ($search1+5),$s);
echo $final;
?>

Link to comment
https://forums.phpfreaks.com/topic/61367-strings/#findComment-305406
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.