0

I have some strings:

$string1 = '<p><strong>Extract me</strong></p><p>Leave me</p>';
$string2 = '<strong>Extract me</strong>Leave me';
$string3 = '<span style="font-weight: bold">Extract me</span><br /><span>Leave me</span>';

Let's check $string3:

The first tag of the string is <span> So the text between the first <span> and the first </span> wants to be extracted.

Extracted shall mean: remove it from $stringX and save it into $extractedX

How would I do this?

iceteea
  • 1,214
  • 3
  • 20
  • 35
  • I tried many things with regex but failed. And I say shame on the guy who wrote the wikipedia-article. – iceteea Apr 27 '12 at 07:03
  • 1
    In general I'd advice against using regex to parse html/xml structures. There are better ways (e.g. [Dom*](http://php.net/manual/book.dom.php), [SimpleXml](http://php.net/manual/book.simplexml.php)). – Yoshi Apr 27 '12 at 07:04
  • Sounds plausible. But I have no idea how to solve my problem in general. – iceteea Apr 27 '12 at 07:06

4 Answers4

2
[^>]*?(?=<\/.*>)

What you should do is use an assertion. [^>]*? searches for any character that is NOT a >. This should be fine since if you need to use > as text, it would need to be escaped as &gt;. Then it searches for the first closing tag as denoted by <\/.*>. The (?=) around it tells the regex engine not to include it in the match.

http://regexr.com?30pkm

Jack
  • 5,680
  • 10
  • 49
  • 74
1

you have to be search first and then post your question here..
any ways here is the related question for your ans Click here to get the releted question

You can done it with preg replace

Community
  • 1
  • 1
chhameed
  • 4,406
  • 4
  • 26
  • 44
1

You can use PHP's preg_match and regular expressions.

This online editor is useful for regex:

http://regexr.com?30pkp:

You'll need something like this to get started:

<strong>(.*)</strong>|<span.+font-weight:\ ?bold.+>(.*)</span>

If you need to do more advanced parsing you could look at parsing the DOM in PHP e.g. using DOMDocument LoadHtml

benedict_w
  • 3,543
  • 1
  • 32
  • 49
0

You can use strip_tags with some use of preg_match if you only want the first occurrence.

Rainulf
  • 341
  • 1
  • 9