1

I have a string in php like this:

Lorem ipsum dolor sit amet
<br />
Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
<hr>
<br />
Ut labore et dolore magna aliquyam erat, sed diam voluptua.
<br />
<hr>
At vero eos et accusam et justo duo dolores et ea rebum.
<hr>
Stet clita kasd gubergren, no sea takimata sanctus est
<hr>
<br />
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet
<hr>
<br />
consetetur sadipscing elitr, sed diam nonumy eirmod tempor
<br />
<hr>
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.

Now after count two <hr> I would like to delete rest of string so it looks like this:

Lorem ipsum dolor sit amet
<br />
Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
<hr>
<br />
Ut labore et dolore magna aliquyam erat, sed diam voluptua.
<br />
<hr>

How can I count and get the secccond<hr>position? I've tried with preg_match() but no chance to get the right result.

Advices are much appreciated, thanks for helping.

2
  • use substr_count($yourstring,'<hr>'); Commented Dec 20, 2013 at 10:19
  • tried substr_count() always gives me 0 :( Commented Dec 20, 2013 at 10:25

2 Answers 2

4

you can use explode for this function like this :

$str = explode("<hr>","Lorem ipsum dolor sit amet
<br />
Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
<hr>
<br />
Ut labore et dolore magna aliquyam erat, sed diam voluptua.
<br />
<hr>
At vero eos et accusam et justo duo dolores et ea rebum.
<hr>
Stet clita kasd gubergren, no sea takimata sanctus est
<hr>
<br />
Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet
<hr>
<br />
consetetur sadipscing elitr, sed diam nonumy eirmod tempor
<br />
<hr>");

$new_arr[] = "<hr>".$str[0];
$new_arr[] = $str[1];
print_r(join("<hr>",$new_arr));
Sign up to request clarification or add additional context in comments.

Comments

2

You can use stripos to find the position of your <hr> then remove all content after the position of the second.

It should not be hard to write ;)

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.