开发者

Pattern Replacement Query. [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklis开发者_如何学JAVAt

Closed 9 years ago.

Improve this question

Currently I have

 Input: e.g., '123456789'.'+'.'987654321'

Desired Pattern:

Output: e.g., '123456789987654321'

How can I achieve this using in php ? I am not through on regex and so what regex would go in preg_replace for this ?


If you only have "123456789+987654321" as the input and want "123456789987654321" as the output, you'd just have to remove the "+" No need for regex:

$output = str_replace("+", "", $input);

If you want to replace this pattern in a bigger string, you can use this regex:

$output = preg_replace("/(\\d+)\\+(\\d+)/", "$1$2", $input);

Note that the "+" has a special meaning in regex and needs to be escaped by a backslash. Additionally, the backslash has a special meaning in PHP strings and needs to be escaped by a backslash itself.

So the above turns into the in-memory string /(\d+)\+(\d+)/, and the regex engine can understand that you mean multiple digits (\d+) and a literal plus (\+).


I am not sure if I get your question, but if you're asking how to get rid of the plus symbol:


$input = '123456789+987654321';
$output = preg_replace('/([^+]+)\+([^+]+)/', '$1$2', $input);
echo $output, PHP_EOL;


Try this one.

 $input = "'123456789'.'+'.'987654321'";
 $output = trim("'", preg_replace("/([^+]+)'\.'\+'\.'([^+]+)/", "$1$2", $input));
 // Without RegEx
 $output = trim("'", str_replace("'.'+'.'", "", $input));
 echo $output;


I think he's actually trying to concatenate

 $customer_id = 123456789;
 $operator_domain = 987654321;

 $Output = $customer_id.$operator_domain;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜