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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.