PHP preg_repalce Replace %var% with text - php

I am trying to replace multiple variables in a text.
I have For example this text:
This is an example text , my variables are:
%name%
%frontname%
%lastname%
%email%
No I want to replace each variable that is between the % % characters with {$variable}.
So my output looks like this:
This is an example text , my variables are:
{$name}
{$frontname}
{$lastname}
{$email}
The pattern I am trying to use is this :
$textResponder = preg_replace('#\%[^\%]+\%#', '{$$1}', $text);
But that is not working, as I get this as output: {$} {$} {$}. Does somebody know what the correct pattern would be?
Thanks in advance

$var = "Hello, %test% World %another test%!";
echo preg_replace('#%(.*?)%#', '{\$$1}', $var);
Test here

Try
preg_replace('/%(.*?)%/', '{\$$1}', $text);

#Gert Van de Ven You need to escape the dollar's special meaning:
$var = "Hello, %test% World!";
echo preg_replace('#\%(.*?)\%#', '{\$$1}', $var);

Related

PHP's str_replace in Python?

Is there anything similar in Python like PHP's str_replace function?
I am looking for something to do this:
string_world = str_replace('Place', "World", "Hello World!")
print (string_world)
Hello Peter!
Specifically something to replace my
str_replace('mp', ' ', $_GET['FML'])
First thing when you are printing "Hello Peter" You should write string_world = str_replace("World", "Peter", "Hello World!") Please check your question.
For String replace in Python, use something like this:
str = "Hello World"
print str.replace("World", "Peter")
print str.replace("World", "Peter", 1) #Parameters: str.replace(find, replace, count)
You can use .replace() on string:
str = "Hello World!"
print str.replace("World", "Place")

str_replace with content not replacing

Hello Stackers,
I'm just having a small PHP Question about the str_replace() Function. When I replace something, it will just replace everything; That's okay. But what I would like to know is this:
str_replace("*", "<strong>", $message);
Is it possible to use str_replace for codes like * This content is Bold *, just having the content, but still replacing the asterisk's with <strong> and </strong>?
Example:
Original: **This Should be Bold**
After Replacing: <strong>This Should be Bold</strong>
For people flagging this as a Duplicate: It's not about closing HTML Tags, it's about replacing.
I Hope I'm not that unclear. Thanks.
Use regular expression instead; it's more convenient:
$message = "**This Should be Bold**";
$message = preg_replace('#\**([^\*]+)\**#m', '<strong>$1</strong>', $message);
echo $message;
Or if you want to limit the number of asteroids to 2:
'#\*{1,2}([^\*]+)\*{1,2}#m'
You can also do like this
https://eval.in/518881
<?php
$string = '**This Should be Bold**';
$string = preg_replace("/\*\*(.+?)\*\*/", "<strong>$1</strong>", $string);
echo $string;
?>

How to ignore single quotes in regex using preg_replace function in PHP?

I am basically trying to transform any hash-tagged word in a string into a link:
Here is what my code looks like:
public function linkify($text)
{
// ... generating $url
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/$1>#$1</a>", $text);
return $text;
}
It works pretty good excepting the case when that $text contains a single quote. Here are
Example1:
"What is your #name ?"
Result: "What is your #name?" Works fine.
Example2:
"What's your #name ?"
Result: "What's your #name?" Does not work, I want
this result: "What's your #name?"
Any idea about how I can get rid of that single quote problem using PHP ?
EDIT1:
Just for info, before or after html_entity_decode($text) I got
"What's your #name?"
Something like this.
$string = "' \'' '";
$string = preg_replace("#[\\\\']#", "\'", $string);
Something is protecting your html entities. This can save your life if the string is coming from a get/post request - but iI it's from a trusted source just use html_entity_decode to convert it back. This 39-thing is a way to express the single quote as you might have realized.
if the problem is html_entities, then maybe you only need to html_entity_decode your $text
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/html_entity_decode($1)>#$1</a>", $text);
Thanks all for your suggestions, I've finally sorted this out with this :
html_entity_decode($str, ENT_QUOTES);

Replace Spaces With + icon

what is the way to replace Spaces With + icon using PHP. Suppose some text like "I Love PHP" will automatically converted like this way "I+Love+PHP" .. assuming a field has this text in a variable like> $text = I Love PHP so this variables text spaces will be replace with a + sign in a new variable like this> $text_plus=I+Love+PHP how to do it with PHP?
Just a guess, but this looks like you're trying to encode the string for a URL, use urlencode().
Using Str_Replace function
In your case it will be :
$text = "I Love PHP";
$output = str_replace(" ", "+", $text);
Try This
str_ireplace(' ','+',$text);

Getting rid of escape characters in PHP

I've got a string containing values such as "hello world\' hello world\'" and I'd like to get rid of the escape characters (the backslashes.)
I've tried the following code:
str_replace("\\", "", $data);
But it doesn't seem to work.
If all you want to do is to get rid of backslashes, then there's a very handy PHP function that accomplishes just that
$var = stripslashes($var);
Assuming you're using $var as the last parameter in str_replace() instead of $data, it should work fine.
$var = "hello world\' hello world\'";
echo $var . "<br />";
echo str_replace("\\", "", $var) . "<br />";
Output:
hello world\' hello world\'
hello world' hello world'
this should work great for you you were not referencing the variable $var correctly in php replace subject parameter also assuming you need to replace the \' you were putting \ which searches for it hence nothing was found to be replaced hope this helps
$var = "hello world\' hello world\'";
echo str_replace("\'","",$var);

Categories