I have code like this:
$comment = preg_replace(
'/\b'.$variable.'\b/',
sprintf(
'#[%s]',
$variable
),
$comment
);
Everything works if I have word like 'Test', when I have 'Test X.A.' my code fails because of dots. I have no idea how to fix it. I try add \ before dot.
Related
This is my variable to be altered:
$last = 'Some string 1 foobar'
and my replace statement
$last = str_replace(['1', '2'], '', $last);
and finally the output
Some string foobar
How do i get rid of the whitespace in between 'string' and 'foobar', my initial thought was that in my str_replace statement using '' as the replacement would also remove the whitespace but it doesnt.
To clarify I want to know how to make it Some string foobar and not Some stringfoobar.
A regular expression based approach is more flexible for such stuff:
<?php
$subject = 'Some string 1 foobar';
var_dump(preg_replace('/\d\s?/', '', $subject));
The output of above code is: string(18) "Some string foobar"
What does that do, how does it work? It replaces a pattern, not a fixed, literal string. Here the pattern is: any digit (\d) along with a single, potentially existing white space character (\s?).
A different, alternative approach would be that:
<?php
$subject = 'Some string 1 foobar';
var_dump(preg_replace('/(\s\d)+\s/', ' ', $subject));
This one replaces any sequence consisting of one or more occurrences of a digit preceded by a white space ((\s\d)+) along with a single white space by a single white blank character.
If you do not want to use preg_replace then you can do something like this.
$result = 'Some string 1 foobar';
$result = str_replace(['1', '2'], '', $result);
$result = str_replace(' ', ' ', $result);
However I have to admit that I like preg_replace solution more. Not sure about the benchmark though.
I'll start off with some code, this is my current setup:
$search = [
'/\{\{\sSTRING\s\}\}/is',
'/\{\{STRING\}\}/is'
];
$replace = [
'<b>TEST_STRING</b>',
'<b>TEST_STRING</b>'
];
echo preg_replace( $search, $replace, "{{STRING}}" );
This would then output TEST_STRING, as wanted, but I'm using two REGEX statements to make this work, and I want this to work with strings like {{ STRING }} as well as {{STRING}} by only using a single REGEX.
I thought the statement would also ignore there being whitespace, but the \s statement specifically looks for anything relating to [\r\n\t\f\v ], is there an expression to use that will ignore whitespace as well as include it?
You can add an optional quantifier "?" after whitespace character "\s".
$search = '/\{\{\s?STRING\s?\}\}/is';
$replace = '<b>TEST_STRING</b>';
echo preg_replace( $search, $replace, "{{STRING}}" );
Hope this helps.
I have a problem with replace \ from string
<?php $postlink = str_replace( "\" , '', $postlink); ?>
what's wrong with that line dreamweaver tell me that wrong line
\ is a special character you need to use \\
<?php
$postlink = str_replace( "\\" , '', $postlink);
?>
You can also use stripslashes() for removing slash, why are you str_replace()? if you have only backslashes than use stripslashes().
Example:
echo stripcslashes("it\'s working day!"); //it's working day!
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".
i write preg match rules:
$subject = 'text LINK text text LINK2';
$search = array(
'/\<a href\="(.*)\">(.*)\<\/a\>/i'
);
$replace = array(
"[a href=\"$1\"]$2[/a]"
);
echo preg_replace($search, $replace, $subject);
When in text only one link everything works great, then more then one - crach code
This i get when is more than one link:
"text [a href="http://google.com">LINK text text "
Change to '/\<a href\="(.*?)\">(.*?)\<\/a\>/i' to make the matching not-greedy.
Here's a better regex - it deals with extra fields in the tags:
\<a (?:.*?)href\=[\"\']([^\"\']+?)[\"\'][^\>]*?\>(.+?)\<\/a\>
I think I've escaped all of the special characters in there, I'm not sure what PHP considers 'special', but basically this should match all of the following:
$subject = 'text <a id="test" href="http://google.com">LINK</a> text text LINK2 text LINK3';
Also, I don't know about PHP, but to match more than one link in Perl, you need the /g modifier on the end of that regex, so:
$search = array(
'/\<a (?:.*?)href\=[\"\']([^\"\']+?)[\"\'][^\>]*?\>(.+?)\<\/a\>/ig'
);
would be your search. Maybe preg_replace does this already, but I'd be surprised, since there are times when you'd only want to replace one instance in your target text.