Jump to content

Getting everything between 2 words in a string


TexasMd91

Recommended Posts

Lets say I have a string with the value

"Robots > Bunnies"

 

Is there any way that I could get everything between "robots" and "bunnies" and put it into its own string

 

Example:

 

<?php
function getInBetween($string, $before, $after, $newstring)
  {
  //function to get whats between $before and $after from $string and put it in $newstring
  }

getInBetween("My name is Mark.","My name is ", ".", $name);
?>

 

SO it would get what is between "My name is " and "." so it would get my name.

 

 

Ofcourse this is just an example and not what I am using it for :P

 

It's vulgar, but...

function get_mid($s, $start, $end)
{
return trim(substr($s, strlen($start), -(strlen($end))));
}

$ss = "test name test";
$n = get_mid($ss, "test", "test");
echo "name: ".$n."<br>";

You could count the strings first and pass the length, esp if repeated!

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.