Syntax for regular expressions in PHP

Source: Internet
Author: User
There are several possibilities for the original:
[font= "Microsoft Yahei" onclick= "alert (' B ')"] People's Republic of China [/font]
[font= "abc" onclick= "Alert (' B ')"] People's Republic of China [/font]
[font= "Microsoft Yahei"] People's Republic of China [/font]
[Font=abc onclick= "alert (' B ')"] People's Republic of China [/font]
[FONT=ABC] People's Republic of China [/font]

I want to do this, I only take out the characters after the font=, but not the onclick (not necessarily the onclick, there may be onfocus and the like) the following characters and the People's Republic of China.
This means that only the font name can be taken out, and the font name may contain quotation marks, but if there are characters behind the font, it is not taken.

The wording of the regular expression is asked.
When I write this, I will take the onclick and the following characters.
/\[font= (. *?) \](.*?) \[\/font\]/i


Reply to discussion (solution)

/\[font= "([^\"]+?) ". +?\](.*?) \[\/font\]/i
Give it a try

/\[font= "([^\"]+?) ". +?\](.*?) \[\/font\]/i
Give it a try


echo preg_replace ('/\[font= ' ([^\ "]+?)". +?\](.*?) \[\/font\]/i ', ' $1@@@@$2 ', ' [font= ', Microsoft Yahei '] People's Republic of China [/font] ');
I tried it, I can't.

echo preg_replace ('/\[font= ' ([^\ "]+?)". *?\](.*?) \[\/font\]/i ', ' $1@@@@$2 ', ' [font= ', Microsoft Yahei '] People's Republic of China [/font] ');

echo preg_replace ('/\[font= ' ([^\ "]+?)". *?\](.*?) \[\/font\]/i ', ' $1@@@@$2 ', ' [font= ', Microsoft Yahei '] People's Republic of China [/font] ');


OK. Thank you.
Excuse me: [^\] What does this string of characters stand for?

In addition, there is a situation that does not match is
[Font=microsoft] People's Republic of China [/font]
This is the one with no quotes on either side of the font. I tried it myself and didn't succeed. Please look again. Thank you.

[^\"]
[] Inside is a set of
^ is anything but what?
It means that any character except the double quotation mark matches, \ is escaped, in fact, can not
---------------------------------------------------------------------------------
[Font=microsoft] People's Republic of China [/font]
'/\[font= ' * ([^\ "]+)" *.*?\ "(. *?) \[\/font\]/i '

Ox break

Thank you for your help. Knot posts

  • 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.