PHP ereg_replace function Base and instance code

Source: Internet
Author: User

String Ereg_replace (String $pattern, String $replacement, String $string)
/*
The modified string is returned. If no matching string is found, then the invariant is returned
*/

Instance

The code is as follows Copy Code
$string = "This 111cn.net a test";
Echo str_replace ("111cn.net", "was", $string);
Echo ereg_replace ("() 111cn.net", "\1was", $string);
Echo ereg_replace ("() 111cn.net)", "\2was", $string);

/*
One thing to note is that if you use an integer parameter value as an alternative, you may not get the results you expect. This is because ereg_replace () will be interpreted as a character value ordinal and applied. For example
*/

The code is as follows Copy Code

$num = 4;
$string = "This string has four words.";
$string = ereg_replace (' Four ', $num, $string);
Echo $string; /* Output: ' This string has words. ' *

/* This'll work. */
$num = ' 4 ';
$string = "This string has four words.";
$string = ereg_replace (' Four ', $num, $string);
Echo $string; /* Output: ' This string has 4 words. '

Look for a ereg_replace to get the connection code

The code is as follows Copy Code
$text = Ereg_replace ("[[: alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"<a href=" ">\0</a>", $text);


Take part of a string

The code is as follows Copy Code
$output = Ereg_replace ("Your regexp Here", "<font color=red>\0</font>", $input);
Print $output;

Let's look at a more complicated example.

The code is as follows Copy Code
function Strip_urls ($text, $reppat)
{
if (! $reppat) {
$reppat = "text [url]";
}
$aimps Tutorial tr = ' php tutorial _strip_urls_function_by_real-php-geek ';
Change $aimps Tutorial TR to anything your want.
$IMPSTR = MD5 ($AIMPSTR);
$text = Str_replace (' </a> ', ' </a> '. $impstr, $text);
$text = Explode ($impstr, $text);
$n = 0;
$texta = Array ();
$reppat = str_ireplace (Array (' text ', ' URL '), Array (' \4 ', ' \2 '), $reppat);
foreach ($text as $text) {
$texta [$n] = Ereg_replace ("<a (. *) href=" (. *) "(. *) > (. *) </a>", $reppat, $text);
$n + +;
}
$TEXTB = Implode ("</a>", $texta);
return $TEXTB;
}

Examples:

The code is as follows Copy Code
$string _of_text = ' <a href= ' http://www.111cn.net/' >php</a> rocks. <a href= "http://www.111cn.net/" > Web production Tutorials </a> also! ';
Echo Strip_urls ($string _of_text, "text");
Echo Strip_urls ($string _of_text, "url");
Echo Strip_urls ($string _of_text, "text [url]");
Echo Strip_urls ($string _of_text, NULL);

/*
Description
Searches the subject for a match in pattern mode and replaces it with replacement. If limit is specified, only the limit match is substituted, and if limit is omitted or the value is-1, all occurrences are replaced.

Replacement can contain a reverse reference in the form of \ n form or (from PHP 4.0.4) $n, preferred to use the latter. Each such reference is replaced with the text that matches the child pattern in the nth captured bracket. n can be from 0 to 99, where the "or" "$" refers to the text that is matched by the entire pattern. Count the left parenthesis from left to right (starting from 1) to get the number of child modes.

A backward reference cannot be represented by a familiar \1 symbol when the replacement pattern is followed by a number (that is, a number immediately following a matching pattern) after a reverse reference. \11, for example, will make preg_replace () want a \1 reverse reference followed by a number 1 or a \11 reverse reference. In this case, the workaround is to use ${1}1. This creates an isolated, reverse reference, and the other 1 is just plain text.

*/

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.