Please go away with php regular expressions

Source: Internet
Author: User
I have been playing with PHP for a few years. When I first came into contact with PHP, I felt that regular expressions were really very foreign. I wanted to replace whatever I wanted. However, as the work goes on, I gradually become disgusted with regular expression Development. the speed of regular expressions is too slow and memory occupied. A netizen in a group said that regular expressions are fast, simple, and highly efficient in development. I didn't argue with him because I thought the same way when I was just doing PHP. But is the regular expression really good? Okay, so I have a few questions and don't need regular expressions. let's see how I solve them.

1. clear all tags in HTML and only keep the hyperlink.
I use strip_tags.
Strip_tags ($ data, $ tags)
$ Data is a string and $ tags is a reserved tag.
Strip_tags ($ data. Finished? Yes, that's simple.
If I want to keep it, strip_tags ($ data ,'')

2. intercept the first match between $ str1 and $ str2 in $ str.

Function str_cut ($ str, $ start, $ end) {// retrieve the first match with the highest efficiency
$ Content = strstr ($ str, $ start );
$ Content = substr ($ content, strlen ($ start), strpos ($ content, $ end)-strlen ($ start ));
Return $ content;
}

3. intercept all matching strings from $ str1 to $ str2 in $ str.

Function my_Ca ($ content, $ start, $ end) {// retrieve all matches, excluding the start and end strings
$ M = explode ($ start, $ content );
$ A = array ();
$ Sum = count ($ m );
For ($ I = 1; $ I <$ sum; $ I ++)
{
$ My = explode ($ end, $ m [$ I]);
$ A [] = $ my [0];
Unset ($ my );
}
Return $;
}

Conclusion: you only need to think more about it and do not need regular expressions. you can solve most of the problems with the built-in functions of PHP.

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.