0

I want to replace with preg_replace with one line.

here's the php file.

<?php
$var_name = 'Firstname Lastname <email@example.com>';
$var_name =  preg_replace('/</', '(', $var_name);
$var_name =  preg_replace('/>/', ')', $var_name);
echo 'OUTPUT: ' . $var_name . "\n";
?>

I get the expected OUTPUT as

OUTPUT: Firstname Lastname (email@example.com)

But, I want to get it with one preg_replace line?

Hope to hear from you.

  • You don't need a regex for this. `str_replace`, only use regexs when needed. `str_replace(array(''), array('(', ')'), $var_name);`. You can't use `if`s in replacement logic, without some sort of callback. – user3783243 Nov 26 '20 at 12:50

0 Answers0