I have a string

$var1 = colors<select><option value = 'blue'></option></select>

I can get the value colors as a string with this statement

$needle1 = "<select>";

$result_string1 = substr("$var1",0,strpos($var1,$needle1));
echo "$result_string1";

will give me

colors.

Then I want to get everything between <select> and </select>

so I use the following code

$needle3 = "</select><br>";
$result_string1a = substr("$var1", strlen($result_string1), strpos($var1, $needle3));

There is a minor problem here.
when I say

echo "$result_string1a";

The expected result is

<select><option value = 'blue'></option></select>

But I get the following

<select><option value = 'blue'></option></select

The </select appears. Since it is not closed with a greater than sign, I have problems with further processing.

If this will definitely happen every time you could just add the greater than:

echo $result_string1a . ">";

very messy but will work.

Or a completely different way using arrays could be

$var1 = "colors<select><option value = 'blue'></option></select>";
$var1_array = explode("<", $var1);
$result_string1 = $var1_array[0];
echo $result_string1;

$var1_array = explode("colors", $var1);
$result_string1a = $var1_array[1];
echo $result_string1a;

the result should be what you need.

commented: Wow, that's very much not a solution. -1

If you are guaranteed that the string is colors<select><option value = 'blue'></option></select> and nothing comes after that, you can just use

substr($var1, strlen($result_string1));

but if you're not, just add 1:

substr($var1, strlen($result_string1), strpos($var1, $needle3) + 1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.