Using PHP replace regex with regex
$input_lines="any word here related to #English must #be replaced.";
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.
Best practices for replacing regex pattern with other regex pattern in php
You can use
preg_replace('~\[test\|(\d+)]~', '<Test id="$1" />', $string)
See the regex demo. Details:
\[test\|
-[test|
substring(\d+)
- Capturing group 1: one or more digits]
- a]
char.
If you need to match any text up to the first ]
rather than just digits, replace \d+
with [^][]*
.
See the PHP demo:
$string = "Cooking lessons\n\n[test|1]\n\n[test|2]";
echo preg_replace('~\[test\|(\d+)]~', '<Test id="$1" />', $string);
Output:
Cooking lessons
<Test id="1" />
<Test id="2" />
(PHP) Replace string of array elements using regex
This expression might also work:
^http\K(?=:)
which we can add more boundaries, and for instance validate the URLs, if necessary, such as:
^http\K(?=:\/\/[a-z0-9_-]+\.[a-z0-9_-]+)
DEMO
Test
$re = '/^http\K(?=:\/\/[a-z0-9_-]+\.[a-z0-9_-]+)/si';
$str = ' http://example1.com ';
$subst = 's';
echo preg_replace($re, $subst, trim($str));
Output
https://example1.com
The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.
RegEx Circuit
jex.im visualizes regular expressions:
PHP replace string using overlapping regular expressions
It's because they don't run simultaneously, but in loop. And after first expression you end up with <b>a</b>bc
so there is no more any abc
for second expression to match.
It's simply as that. And that's good, because thanks to that you won't end with invalid markup. You will. Regex is serious problem with HTML or XML.
Use some DOM interpreter and library, like PHP's DOMDocument
PHP Regex replace all instances of a character in similar strings
Not as elegant but you could create a UDF:
function RemoveNestedQuotes($string)
{
$firstPart = explode(":", $string)[0];
preg_match('/"(.*)"/', $string, $matches, PREG_OFFSET_CAPTURE);
$tmpString = $matches[1][0];
return $firstPart . ': "' . preg_replace('/"/', '', $tmpString) . '"';
}
example:
$string = 'Data_83: "He said, "Yes!" to the question"';
echo RemoveNestedQuotes($string);
// Data_83: "He said, Yes! to the question"
PHP: Regex replace everything between to strings/HTML tags
The reason you're getting the error is because you've not escaped an opening [
character in your regular expression. Please see the [
I have marked below:
preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>[\/quote\]\<\/p\>/', '', $string);
^
This has resulted in starting a character class that has not been closed. You should simply escape this opening brace like this:
preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>\[\/quote\]\<\/p\>/', '', $string);
Simple PHP regex replace
Try this one. Here we are using preg_replace
.
Search:
/\.(\d+)\./
Added+
for capturing more than one digit and changed capturing group for only digits.Replace:
.<BR>$1.
$1
will contain digits captured in search expression.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = "1. Place pastry on microwave safe plate.2. Heat on high for 3 seconds.3. Cool briefly before handling.";
echo preg_replace("/\.(\d+)\./", ".<BR>$1.", $string);
PHP Regex Replace Subject while keeping case
If I had more time there might be a more elegant solution, but here's a start:
foreach ($delta as $k => $v) {
$pattern = '/\b(?<!\-)('.$k.')\b(?!-)/i';
$result = preg_replace_callback($pattern,
function($m) use($v){
return (ucfirst($m[1]) == $m[1]) ? ucfirst($v) : $v;
}, $str);
}
This will not account for all upper or mixed cases such as AUTHORIZE
and authORize
etc...
Related Topics
Facebook Oauthexception: "User Hasn't Authorized the Application to Perform This Action"
How to Use Facebook Graph API to Retrieve Fan Photos Uploaded to Wall of Fan Page
Pdo's Rowcount() Not Working on PHP 5.2.6+
Setting $_Session Doesn't Work on Localhost Using Xampp
Move Value in PHP Array to the Beginning of the Array
Replace the Last Comma in a String Using Regular Expression
Problems with Secure Bind to Active Directory Using PHP
Display Zend_Form_Element_Radio on One Line
PHP $String{0} VS. $String[0];
Check If Username Exists in MySQL Table via PHP
How to Read the Query String in PHP and HTML
Mysql: Get Total in Last Row of MySQL Result
Check If Method Exists in the Same Class
Why Ob_Start() Must Come Ahead of Session_Start() to Work in PHP
PHP Making a Download Counter Without Leaving the Current Page