-2

I have a string like this:

$string = 'this is some random text 
  <div class="myClass" title="myTitle">this is some random text
  again</div> and some more randome text';

Now I would like to remove this content from it:

<div class="myClass" title="myTitle">this is some random text again</div>

But at the same time I would like to store the value of class and the value of title

Any ideas on how to go about this?

matt
  • 2,312
  • 5
  • 34
  • 57

1 Answers1

1

Here's a sample script for what your example shows specifically:

<?php
  $string = 'this is some random text <div class="myClass" title="myTitle">
             this is some random text again</div> and some more randome text';
  preg_match('/class=\"(\w{1,})\"\stitle=\"(\w{1,})\"/i', $string, $matches);
  echo "Class: {$matches[1]}\n";
  echo "Title: {$matches[2]}\n";
?>

Output:

marks-mac-pro:~ mstanislav$ php regex.php 
Class: myClass
Title: myTitle

You may have to adjust for other uses and/or ensure compatibility with naming conventions of HTML attributes.

Mark Stanislav
  • 979
  • 4
  • 11