0

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.

Edited by raghujosh: n/a

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by twiss
-1

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.

Votes + Comments
Wow, that's very much not a solution.
0

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);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.