0

I got this

$description =  '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>'

I want to remove only what comes after <p>text3</p>

My result would be:

$description = '<p>text1</p><p>text2</p><p>text3</p>'

My guess is that we need to use preg_replace with some regex but I can't manage to write a working one.

Wistar
  • 3,770
  • 4
  • 45
  • 70
  • 3
    It's almost 2013. Use an XML parser. –  Dec 31 '12 at 19:43
  • 1
    Never use regex to parse XML. You might cause a mental meltdown of some random developer somewhere in the future ... or in the past. – John Dvorak Dec 31 '12 at 19:44
  • 1
    See http://stackoverflow.com/a/1732454/499214 – John Dvorak Dec 31 '12 at 19:46
  • regex is probably not the best tool for this job but I would think if your markup is uniform then a regex could work. You haven't given us the structure of the document so it's hard to come up with a regex. I don't know preg syntax off the top of my head but something like `((

    [^<]*

    ){3}).*` as a group and then replace with `\1`
    – User Dec 31 '12 at 19:50
  • This is the tool for you: http://php.net/manual/en/class.domdocument.php – John Dvorak Dec 31 '12 at 19:58
  • @Jan Dvorak: sometimes pragmatism > principle. Here's my aircode version: `$cleanDescription = preg_replace('/((

    [^<]*

    ){3}).*/i','$1', $description);` Obviously Wistar there are a number of problems with using regex but it might be okay for your use-case. Just depends. Best thing would be to encapsulate the functionality in a function and then you can change the implementation as needed.
    – User Dec 31 '12 at 20:07
  • 2
    @User This will fail horribly (do nothing at all) if the `p` tags are unclosed (which is valid HTML) or contain other tags. – John Dvorak Dec 31 '12 at 20:09

1 Answers1

2

You could...

function str_occurance($needle, $haystack, $occurance) { 
$occurance += 2;
$arr = explode($needle, $haystack, $occurance);
unset($arr[0]);
$arr = array_values($arr);
$key = count($arr) - 1;
unset($arr[$key]);
$str = $needle . implode($needle, $arr);
return $str; 
}

Not the prettiest, but it works.

Edit: To use:

$description =  '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>';
$split = '<p>';
$return = 3;
$new = str_occurance($needle, $description, $return);
echo $new; // returns <p>text1</p><p>text2</p><p>text3</p>
Josh Brody
  • 5,153
  • 1
  • 14
  • 25