<?php
$text = 'abcdefghijklmnopqrst';
$newfilename = preg_match('/ij(.*?)mn/', $text, $out);
echo $out[0];
echo $out[1];
?>
[1] -gives only the inner search result (what we had in the brackets, between "ij" and "mn"):
kl
[0] -gives the whole search result:
ijklmn
<?php
$text = 'Hello user Jimmy Jones, it\'s me. Hello user Mery Pawders, it\'s me.';
$newfilename = preg_match_all('/hello user (.*?) it\'s/', $text, $out);
foreach ($out[1] as $found_one) {
echo $found_one;
}
// Or use $out[0] for full search match
?>
Output will be:
Jimmy Jones,
Mery Pawders,
<?php
$text = 'Hi, it\'s me, Niko from Austria';
$variable = preg_replace('/(Niko.*?) from/', '$1 Gomez', $text);
echo $variable;
?>
Output will be:
it's me, Niko Gomez Austria
实施例11):查找文本中的所有链接:
<?php
$text = 'hi, my site is http://example.com, and on my page, at http://example.com/page37/blabla.html I wrote something..';
preg_match_all("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",$text, $out);
foreach($out[0] as $varr){
echo $varr;
}
?>
Output will be:
http://example.com
http://example.com/page37/blabla.html
实施例12):与示例 11 类似(但带有替换) - 查找文本中的链接并将其放入锚定标签中:
<?php
$text = 'Hi, my site is http://example.com, and on my page, at http://example.com/page37/trid.html I wrote something..';
$variable = preg_replace("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",'<a href="\\0">\\0</a>', $text);
echo $variable;
?>