1

I am looking for a regular expression (using preg_split()) that can be used to split the following string:

hello<span id="more-32"></span>world                 

Desired result:

$var[0]=hello 
$var[1]=world

I tried using this code put it didn't work

preg_split('/<span id="more-\d+"></span>/','hello<span id="more-32"></span>world')
3
  • 5
    Why not simply strip_tags()? Commented Sep 13, 2013 at 17:34
  • Regex isn't always the answer. Commented Sep 13, 2013 at 18:14
  • because i need the result to be back in array and i need to know what is before the span and what is after it Commented Sep 13, 2013 at 18:31

1 Answer 1

1

First: You should escape the fore slash by a backslash.

Second: You should put the semicolon at the end of code.

This will work:

    <?php
    $string = 'hello<span id="more-32"></span>world';
    $pattern = '/<span id="more-\d+"><\/span>/';

    $out = preg_split($pattern,$string);

?>

Print the splitted string:

    foreach ($out as $value) {
    echo $value . '<br />'; 
}

Output:

hello
world

or:

print_r($out);

Output:

Array (
     [0] => hello 
     [1] => world 
) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.