-1

If we take a text Like this

 <p>Portable <span class="shlt">Adobe</span> <span class="shlt">After</span>
 <span class="shlt">Effects</span> CC <span class="shlt">2018</span> 15.1.1.12 (x64)</p>

There are words between those <span class="shlt"></span> tags. I need to capture The title Only!

(You can clearly see that it contains Portable Adobe After Effects CC 2018 15.1.1.12 (x64))

Is it possible to avoid capturing <span class="shlt"> and </span> Parts?

And Capture only the Portable Adobe After Effects CC 2018 15.1.1.12 (x64) Text?

What I am currently trying to do is Capturing the words in between those tags. Is there a better way! A sample regex Code will be useful. In PHP Please...

2 Answers2

2

Instead of using a regex, you might use DOMDocument and use getElementsByTagName to find your <p> element.

Then take the first match from the result and get the textContent:

$dom = new DOMDocument();
$dom->loadHTML($data);
echo $dom->getElementsByTagName("p")[0]->textContent;

That will give you:

Portable Adobe After Effects CC 2018 15.1.1.12 (x64)
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Doesn't it also give those span tags.Also I do not have the Document File I need. I am just using cURL to get it... – The Bang Bandit Jun 01 '18 at 16:52
  • According to the [docs](http://at.php.net/manual/en/class.domnode.php#domnode.props.textcontent), this returns `The text content of this node and its descendants.` – The fourth bird Jun 01 '18 at 16:59
  • But How do i use it if i was using cURL?? – The Bang Bandit Jun 01 '18 at 17:02
  • Then you load the html from the curl request into DOMDocument. When the response is a more complicated structure, you could then use [DOMXPath](http://php.net/manual/en/class.domxpath.php) and create an xpath expression with [query](http://php.net/manual/en/domxpath.query.php). – The fourth bird Jun 01 '18 at 17:08
0

You can capture groups inside of the regex by using (). Then you can parse out the array.
Here is an example.

$re = '/\<span class="shlt">([^<]*)<\/span>/m';
$str = 'Portable <span class="shlt">Adobe</span> <span 
class="shlt">After</span> <span class="shlt">Effects</span> CC <span 
class="shlt">2018</span> 15.1.1.12 (x64)';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

this will remove the span tags

$str = 'Portable <span class="shlt">Adobe</span> <span 
class="shlt">After</span> <span class="shlt">Effects</span> CC <span 
class="shlt">2018</span> 15.1.1.12 (x64)';

preg_replace("/<\/?span[^>]*>/", "", $str);
echo $str;
jarchuleta
  • 1,231
  • 8
  • 10