-1

I have this code:

$body = '<p>awesometext:data</p>';
preg_match_all("/<p>([^<]+)<\/p>/", $body, $matches);
$string = array_map('trim', $matches[1]);

In result I get empty array $string. How I can fix it?

String can be is more. Example:

$body = '<p>awesometext:data</p><p>othertext:data</p><p>sss:sddd</p>'; //e.t.c
Dumitru
  • 1,463
  • 1
  • 12
  • 33

1 Answers1

0

If I understood you correctly, to get the strings inside the p tag you can:

let str = '<p>awesometext:data</p><p>othertext:data</p><p>sss:sddd</p>';
let matches = str.replace(/<p>|<\/p>/gi, ' ').trim().match(/([a-z]+?:[a-z]+)/gi);

matches would return an array of strings. Please note the above expression doesn't solve for edge cases. i.e leading/trail spaces, etc.

nullhook
  • 510
  • 5
  • 10