views:

52

answers:

4

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 ?

A: 

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;
janmoesen
Input is in the form '123456789'.'+'.'987654321' and so current regex wont give desired result.
Rachel
`'123456789'.'+'.'987654321'` is the same as `'123456789+987654321'`. Am I missing something?
janmoesen
can you explain the regex used, so I can learn some about regexes ?
Rachel
@janmoesen, he's looking for the actually quote "'123456789'.'+'.'987654321'"
St. John Johnson
@St.John Johnson: Yes. You are right.
Rachel
A: 

Try this one.

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

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 (\+).

Tomalak
A: 

I think he's actually trying to concatenate

 $customer_id = 123456789;
 $operator_domain = 987654321;

 $Output = $customer_id.$operator_domain;
mcgrailm
Yes. But am not getting two strings seperately, I have string1+string2 and I want to concatenate it to string1string2
Rachel