What should I do if I only want to keep the first regular expression string in PHP?

Source: Internet
Author: User
What should I do if I only want to keep the first regular expression string in PHP? PHP
For example, there are many asd strings, several consecutive strings, and only one string,

But I want to keep only the first asd and delete all the asd behind it. what should I do?

(Based on other characters not affected)


Ssssssssgsdfasdasdasdjlkjhkjhalkasdasdjsdkajshdasdaklsjdasdhkjashgdaklsh

Expected results:



Ssssssssgsdfasdjlkjhkjhalkjsdkajshdaklsjdhkjashgdaklsh

Please give me some tips


Reply to discussion (solution)

$ S = 'handler'; $ t = 'handler'; $ s = preg_replace_callback ('/asd/', function ($ r) {static $ n = 0; return! $ N ++? $ R [0]: '';}, $ s); var_dump ($ s = $ t );
Bool (true)

I have no time in a day. thanks very much to the moderator. Yes, this is the effect. it turns out that there is something as useful as preg_replace_callback. does preg_replace not have this function? Moderator

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.