Dollar($) sign not working in Email template - php

I have built a custom email template. And assigned some variables with {#paid_amount} and so on.
All the variables get replaced but paid_amount not as expected. I have replaced something like this:
// Text file with HTML markups
$template = file_get_contents($template_url);
$paid_amount = '$1.00';
$pattern = array(
'/\{\#user_name\}/i',
'/\{\#paid_amount\}/i',
'/\{\#duration\}/i' );
$replacement = array(
$user_name,
$paid_amount,
$duration );
$new_template = preg_replace($pattern, $replacement, $template);
Its print the amount .00 in the email, and if i remove the sign $ from the amount it print the 1.00. I tested it in Gmail. Has anyone faced this before?
Even i tried with $ but not working. Can anyone please tell me what i have missed or why it is not working?

You need to escape the dollar sign:
$paid_amount = '\$1.00';
This is because preg_replace() is using the $ in the replace parameter to address the contents of a capturing group.
Example:
$string = ">> hello <<";
$pattern = "/>> ([^ ]*) <</";
echo preg_replace($pattern, '$1', $string);
In the above example, $1 addresses the contents of the first capturing group: ([^ ]*) -> "hello".

Related

string replace two matches that have an exact (partly) match within one string

I have a variable $text which is a plain text that can contain one or more email addresses in a line of text. I use a regular expression to find these email addresses and then transform them into clickable <a href="mailto:....etc addresses. This is my code with an example that work fine:
$text = "this is the text that has a email#email.com in it and also test#email.com.";
if(preg_match_all('/[\p{L}0-9_.-]+#[0-9\p{L}.-]+\.[a-z.]{2,6}\b/u',$text,$mails)){
foreach($mails[0] as $mail ){
$text = str_replace($mail,''.$mail.'',$text);
}
}
Or see this live demo. Problems occur when in my variable $text there are two email adresses that have an exact (partial) match. For example sometest#email.com and test#email.com. Here's another live demo. The problem is the string replace happens within the partial match as well (because it is also a full match). How to bypass this issue?
Why not use preg_replace?
str_replace can overwrite previous matches.
This should be good for you:
echo preg_replace(
'/([\p{L}0-9_.-]+#[0-9\p{L}.-]+\.[a-z.]{2,6}\b)/u',
'$1',
$text
);
Notice that I had to slightly modify the regular expression and wrap it in parentheses.
This is so that I can reference it in the replacement.
Live demo
You have to catch the caracter before your match to be sure it's a full match :
if(preg_match_all('/(.)([\p{L}0-9_.-]+#[0-9\p{L}.-]+\.[a-z.]{2,6}\b)/u',$text,$mails))
-----------------------------------^
Then you just have to modify a bit your str_replace parameter
var_dump($mails);
$id = 0;
foreach($mails[2] as $mail ){
$text = str_replace($mails[1][$id].$mail,'$mails[1][$id].'.$mail.'',$text);
$id ++;
}
For example : https://3v4l.org/qYpHo
Like so...
<?php
$string = "this is the text that has a email#email.com in it and also test#email.com.";
$search = array ( "!(\s)([_\.0-9a-z-]+#([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})!i",
"!^([_\.0-9a-z-]+#([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})!i" );
$replace = array ( "\\1\\2",
"\\1" );
echo preg_replace ( $search, $replace, $string );
?>
result...
this is the text that has a email#email.com in it and also test#email.com.

PHP regex preg_replace function Joomla! plugin

I want to create plugin that finds and replaces phone numbers with a linked number for mobile. This is a php function that I have written for Joomla to use to replace a phone number...
protected function clickToCall(&$text, &$params){
// phone number pattern...
$pattern = '~(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}~';
//replacement pattern...
$replacement = '$1$2';
//use preg_replace to actually replace the pattern
$text = preg_replace($pattern, $replacement, $text);
//return the new value
return true;
}
Right now the function finds the pattern and just replaces it with an empty link. How can insert the phone number found by regex into a link?
The problem is that you are using $1 and $2 backreferences that refer to Group 1 (i.e. substring captured with (\+0?1\s)?) and non-existent Group 2 (thus, it is an empty string) rather than to the whole match (that can be referred to with the help of $0 or \0).
Here is a fix:
$text = 'Phone +1 (234) 345 6543 to obtain a free copy.';
$pattern = '~(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}~';
$replacement = '$0';
echo $text = preg_replace($pattern, $replacement, $text);
See IDEONE demo
See more about Back references in PHP Manual.

Converting Npx to [N/10]rem with regex

I have php code that writes a string, which is actually an html file, to the server, but before the write I want to rip through and replace all "Npx" with "[N/10]rem". So "width:203px" would become "width:20.3rem" and top:46px" would become "top:4.6rem". Does anyone see a regex string that will do this?
Thanks
Just capture the digit before px then match the string px and replace all the chars with .$1rem
. Where $1 refers to the characters which are present inside the group index 1.
(\d)px
Replacement string:
.$1rem
DEMO
$string = <<<EOT
width:203px
top:46px
top:6px
EOT;
$pattern = "~(\d)px~";
$replacement = ".$1rem";
echo preg_replace($pattern, $replacement, $string);
Output:
width:20.3rem
top:4.6rem
top:.6rem

Regex replacement with multiple occurances and different values within a string in PHP

I have this code:
<object height="510" width="650">height="510"width="650">
or this code:
<objectheight="345"width="123'> height='456'width=789>
The quotes around the values could be double, single or none. And the words could be put together or there could be one or more whitespaces. And what is important the digits as a value could be anything
So, I need to replace the integervalue in height="510" (or height='123' or height=456) with my own variable $height_val.
My code so far:
$height_val = "640";
$pattern = ??? // this regex I need to find out
$replacement = $height_val;
$string = '<objectheight="345"width="123\'> height=\'456\'width=789>'
$result = preg_replace($pattern, $replacement, $string);
And the final result should be <objectheight="640"width="123\'> height=\'640\'width=789>
The reg-ex I'd use is: height=(["']*)[0-9]*(["']*). This ensures you only get the height value with any non-alpha digit after the equals followed by an any length number.
$height_val = "640";
//$pattern = '/height=\D[0-9]*\D/' // pre comments regex
$pattern = height='/(["']*)[0-9]*(["']*)/'; //new regex
$replacement = $height_val;
$string = '<objectheight="345"width="123\'> height=\'456\'width=789>'
$result = preg_replace($pattern, $replacement, $string);
I've tested it on the following variables:
<object height="510" width="650">height="510"width="650">
<objectheight="510" width="650">height="510"width="650">
<object height='510' width="650">height="510"width="650">
<objectheight='510'width="650">height="510"width="650">
value="width=650&height=515&plugins=http://
Going forward I would recommend you try using a RegEx tester to try your own combinations. You can also use this regex reference to give you help with character classes.
Updated:
If you wish to allow for no quotation marks too use the following:

Regex "1 Month, -$7.50" to replace "-$7.50" with "-10%"

This is my attempt, but it's not working. I'm so rusty at regex these days so I have no idea what's wrong with it. Any help?
$pattern = '/-\$[.*]$/';
You need to put the * outside of the [] (or better yet, use a +).
Try '/-\$[0-9\.]+/':
$pattern = '/-\$[0-9\.]+/';
Or, if -$7.50 will always be at the end of the line:
$pattern = '/-\$[0-9\.]+$/';
$pattern = '/-\$(.*)$/';
If I understood well. The number will be accessible as "$1" in the replace string
Try with:
$pattern = '/-\$\d+\.\d+/';
It will match minus sign (if you change it into -?, minus will be optional), digits, dot and digits after dot.
<?php
$string = 'Wow! -$7.50 now!';
$pattern = '/-\$(\d+)\.(\d+)/';
$replacement = '-10%';
echo preg_replace($pattern, $replacement, $string);
?>

Categories