简体   繁体   中英

Regex get string in tag <p></p>

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

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. ie leading/trail spaces, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM