$input_lines="any word here related to #English must #be replaced.";
$result = preg_replace("/(#\w+)/", "<a href='bla bla'>$1</a>", $input_lines);
DEMO
OUTPUT:
any word here related to <a href='bla bla'>#English</a> must <a href='bla bla'>#be</a> replaced.
Answer from Nambi on Stack OverflowPHP
php.net › manual › en › function.preg-replace.php
PHP: preg_replace - Manual
preg_replace() returns an array if the subject parameter is an array, or a string otherwise. If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or null if an error occurred.
W3Schools
w3schools.com › php › func_regex_preg_replace.asp
PHP preg_replace() Function
❮ PHP RegExp Reference · Use ... » · The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings....
09:34
PHP Regular Expressions (Regex) Tutorial: Advanced Pattern Matching ...
06:48
PHP: Replacing Text (preg_replace) - YouTube
04:57
PHP Function preg_replace() - YouTube
02:50
Replace parts of strings using regular expressions in PHP - YouTube
07:29
Beginners Intro to "Find and Replace" with Regular Expressions ...
08:34
59: Functions Using Regular Expressions | PHP Tutorial | Learn ...
Top answer 1 of 3
52
$input_lines="any word here related to #English must #be replaced.";
$result = preg_replace("/(#\w+)/", "<a href='bla bla'>$1</a>", $input_lines);
DEMO
OUTPUT:
any word here related to <a href='bla bla'>#English</a> must <a href='bla bla'>#be</a> replaced.
2 of 3
8
This should nudge you in the right direction:
echo preg_replace_callback('/#(\w+)/', function($match) {
return sprintf('<a href="https://www.google.com?q=%s">%s</a>',
urlencode($match[1]),
htmlspecialchars($match[0])
);
}, htmlspecialchars($text));
See also: preg_replace_callback()
Phpliveregex
phpliveregex.com
PHP Live Regex
Test PHP regular expressions live in your browser and generate sample code for preg_match, preg_match_all, preg_replace, preg_grep, and preg_split!
Rexegg
rexegg.com › regex-php.php
PHP Regex Tutorial
In such cases, str_replace can be faster than the preg_replace regex function: $string=str_replace('10','20','$string'); The preg_replace function comes in when you need a regex pattern to match the string to be replaced, for instance if you only wanted to replace '10' when it stands alone ...
GeeksforGeeks
geeksforgeeks.org › php › php-preg_replace-function
PHP | preg_replace() Function - GeeksforGeeks
July 11, 2025 - Program 1: The following program replaces string by using the regular expression parameter. ... <?php // PHP program to illustrate // preg_replace function $string = 'November 01, 2018'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1} 02, $3'; // print output of function echo preg_replace($pattern, $replacement, $string); ?>
Medium
captainnoob.medium.com › command-execution-preg-replace-php-function-exploit-62d6f746bda4
Command Execution - preg_replace() PHP Function Exploit | RCE
October 10, 2020 - After executing this , preg_replace() the search for `as` and replace with `As` . ... To exploit the code, all the attacker has to do is provide some PHP code to execute, generate a regular expression which replaces some or all of the string with the code, and set the `e` modifier on the regular expression/pattern
Smarty
smarty.net › docs › en › language.modifier.regex.replace.tpl
regex_replace | Smarty
A regular expression search and replace on a variable. Use the preg_replace() syntax from the PHP manual. Although Smarty supplies this regex convenience modifier, it is usually better to apply regular expressions in PHP, either via custom functions or modifiers.
W3Schools
w3schools.com › php › php_regex.asp
PHP Regular Expressions
flush() ob_clean() ob_end_clean() ob_end_flush() ob_flush() ob_get_clean() ob_get_contents() ob_get_flush() ob_get_length() ob_get_level() ob_gzhandler() ob_implicit_flush() ob_list_handlers() ob_start() output_add_rewrite_var() output_reset_rewrite_vars() PHP RegEx · preg_filter() preg_grep() preg_last_error() preg_match() preg_match_all() preg_replace preg_replace_callback preg_replace_callback_array preg_split preg_quote PHP SimpleXML ·
Top answer 1 of 5
11
A correct answer to this question has been provided previously:
Look at the second answer:
PHP: insert text up to delimiter
In addition, your implementation is wrong, look at the regular expression it should start with ^ :
echo preg_replace('/(.*?):/', "<font color=#F00>$1</font>:", $output);
Should be:
echo preg_replace('/^(.*?):/', "<font color=#F00>$1</font>:", $output);
2 of 5
1
try:
echo preg_replace('/^(.*?):(.*?)$/s', "<font color=#F00>\\1</font>:\\2", $output);
EDIT: This should work (tried it):
trim(preg_replace("/(?:\n)(.*?):(.*?)/s", "<font color=#F00>\\1</font>:\\2", "\n".$str))
Final try, maybe try to explode it instead:
<?php
$content = 'name: some text
name2: more text
name: text
name3: text';
$tmp = explode("\n", $content);
for($i = 0; $i < count($tmp); $i ++) {
$tmp[$i] = '<span style="color:#F00">'.str_replace(':', '</span>:', $tmp[$i], 1);
}
echo implode("\n", $tmp);
?>
This does assume that whatever is before the colon, it won't have another colon.
My bad, I misunderstood str_replace()'s last parameter. Try this:
<?php
$tmp = explode("\n", $content);
for($i = 0; $i < count($tmp); $i ++) {
$tmp2 = explode(':', $tmp[$i]);
$tmp2[0] = '<span style="color:#F00">'.$tmp2[0].'</span>';
$tmp[$i] = implode(':', $tmp2);
}
echo implode("\n", $tmp);
Medium
medium.com › @wearedopethemes › replacing-ereg-replace-with-preg-replace-in-php-for-regex-handling-e4cdf53450e2
Replacing ereg_replace() with preg_replace() in PHP for Regex Handling | by DopeThemes.com | Medium
September 3, 2024 - Case Sensitivity: Unlike ereg_replace(), which is case-insensitive by default, preg_replace() requires you to specify the i modifier for case-insensitive matches, giving you more control over your regex patterns. Compatibility: Since ereg_replace() has been deprecated and removed from PHP, continuing to use it in your code can prevent you from upgrading to newer versions of PHP, exposing your applications to security risks and missing out on performance improvements.
Reintech
reintech.io › blog › understanding-implementing-php-preg-replace-function
Understanding and Implementing PHP's `preg_replace()` Function
April 14, 2023 - Learn how to use PHP's preg_replace() function for string manipulation tasks, including working with regular expressions and callback functions.
AWS
aws.amazon.com › blogs › industries › building-observability-on-amazon-managed-grafana-for-5g-o-ran-sites-built-on-eks-anywhere
Building Observability on Amazon Managed Grafana for 5G O-RAN sites built on EKS-Anywhere | Amazon Web Services
January 25, 2024 - An important step in the push for an open, virtualized, and intelligent 5G has been the proliferation of Open Radio Access Network (O-RAN), which seeks to improve Radio Access Network (RAN) flexibility and deployment velocity, while simultaneously reducing the capital and operating costs through ...
NGINX
nginx.org › en › docs › beginners_guide.html
Beginner’s Guide
nginx can be used to route requests ... such as PHP. The most basic nginx configuration to work with a FastCGI server includes using the fastcgi_pass directive instead of the proxy_pass directive, and fastcgi_param directives to set parameters passed to a FastCGI server. Suppose the FastCGI server is accessible on localhost:9000. Taking the proxy configuration from the previous section as a basis, replace the proxy_pass ...
Nusphere
nusphere.com › kb › phpmanual › function.preg-replace.htm
PHP Manual: preg_replace
PhpED - PHP IDE integrated development environment for developing web sites using PHP, HTML, Perl, JScript and CSS that combines a comfortable editor, debugger, profiler with the MySQl, PostrgeSQL database support based on easy wizards and tutorials.Easy to use for debugging PHP scripts, publishing ...