如何使用preg_replace?

时间:2015-08-18 11:42:32

标签: php

如何使用preg_replace获取变量的特定文本:

$text = "hello micle, your transaction id 1313xxx success. SN: 525252xxx. your balance is $10 @18/08 14:14:51#";

比我需要preg_replace返回id : 1313xx, success status, SN, and balance.

1 个答案:

答案 0 :(得分:0)

这个会对你有所帮助。

$specified_txt="Arun";
if(stripos($specified_txt,$specified_txt)!==false)
{
    $string = 'Hi Arun How Are You';
    $pattern = '/'.$specified_txt.'/';
    $replacement = 'Sarvanan';
    echo preg_replace($pattern, $replacement, $string);
}

echo "<br>";

$string = 'php mysql jquery javascript xml html css ajax.';
$patterns = array();
$patterns[0] = '/php/';
$patterns[1] = '/mysql/';
$patterns[2] = '/jquery/';
$replacements = array();
$replacements[2] = 'Java';
$replacements[1] = 'sql';
$replacements[0] = 'AngularJs';
echo preg_replace($patterns, $replacements, $string);
相关问题