Ask PHP to use a regular expression string, just want to keep the first what to do

Source: Internet
Author: User
Does PHP use regular expression strings, just want to keep the first one to do?
Php
For example, there are many ASD for this string, there are several consecutive, there are just one,

But I want to keep only the first ASD, and all the ASD in the back are erased.

(on the basis of not affecting other characters)


Ssssssssgsdfasdasdasdasdjlkjhkjhalkasdasdjsdkajshdasdaklsjdasdhkjashgdaklsh

Desired Result:



Ssssssssgsdfasdjlkjhkjhalkjsdkajshdaklsjdhkjashgdaklsh

Ask the master to give some hints

Share to:


------Solution--------------------
$s = ' ssssssssgsdfasdasdasdasdjlkjhkjhalkasdasdjsdkajshdasdaklsjdasdhkjashgdaklsh ';
$t = ' Ssssssssgsdfasdjlkjhkjhalkjsdkajshdaklsjdhkjashgdaklsh ';
$s = preg_replace_callback ('/asd/', function ($r) {
static $n = 0;
Return! $n + +? $r [0]: ";
}, $s);
BOOL (TRUE)
  • 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.