Regular Expressions-PHP regular substitution problem

Source: Internet
Author: User
My regular comparison dish, there is now a regular replacement of the problem has been uncertain, the demand is this:

Some of the tags in the content

(这里有任意内容和标签)( 这里也有任意内容和标签)

Then the item is replaced with this:

(这里有任意内容和标签)( 这里也有任意内容和标签)

Simply said that the img outside the bread figure and then pulled out to put behind p, and then use preg_replace how to write it?

Reply content:

My regular comparison dish, there is now a regular replacement of the problem has been uncertain, the demand is this:

Some of the tags in the content

(这里有任意内容和标签)( 这里也有任意内容和标签)

Then the item is replaced with this:

(这里有任意内容和标签)( 这里也有任意内容和标签)

Simply said that the img outside the bread figure and then pulled out to put behind p, and then use preg_replace how to write it?

Can I understand that

    • Extract all the img tags under p to p, if it is multiple img, this needs to use Preg_match.

    • And there will be other labels in p, except P tags

If there's only one img inside,

//正则$p = '#
  <>

.*?)>(? .*?)(?\)(? .*?)

#i';//替换$r = ' $2$4

$3 ';//原内容$s = '

fsdfsdfsfkolja;ad;lasd

';echo preg_replace($p, $r, $s);// 结果://

fsdfsdfsfkolja;ad;lasd

There are multiple img

$s = '

fsdfsdfsfkolja;ad;lasd

asdaa

';echo preg_replace_callback('# <>

.*?)>(? .*?)

#', function($matches) { preg_match_all('##', $str = $matches[0], $matches1, PREG_OFFSET_CAPTURE ); foreach (array_reverse($matches1[0]) as $v) $str = substr_replace($str, '', $v[1], strlen($v[0])); return $str.'

'.implode('',array_map(function($v){return $v[0];}, $matches1[0])).' ';}, $s);// 结果://

fsdfsdfsfkolja;ad;lasd

asdaa

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.