With word boundaries (\b) in RegEx do I need to have it before AND after the word, or just before?
You would need to use both. Without the last \b
you would get a match on strings such as:
"I love football"
"You foolishly left off your second word boundary"
php regex [b] to b
There are various BBCode parsers available for PHP, for instance
- http://www.php.net/manual/en/book.bbcode.php
which allows you to simply define your replacement rules by hand:
echo bbcode_parse(
bbcode_create(
array(
'b' => array(
'type' => BBCODE_TYPE_NOARG,
'open_tag' => '<b>',
'close_tag' => '</b>'
)
)
),
'[b]Bold Text[/b]'
);
// prints <b>Bold Text</b>
Also check the various similar questions about BBCode Parsers:
- https://stackoverflow.com/search?q=bbcode+php
PHP Regex Word Boundary exclude underscore _
You would have to create DIY boundaries.
(?:\b|_\K)foo(?=\b|_)
Dollar Sign with \b in regex includes extra space
Simply try to find numbers that are not prefixed by $
, and replace those with that prefix character, followed by whatever you want, !
in your example.
$test_string = "This is a number 100 but this isn't \$100.";
$result = preg_replace('/([^\$\d])(\d+)/', '\1!', $test_string);
var_dump($result);
Regular expression - PCRE (PHP) - word boundary (\b) and accent characters
It will work, when you add the u
modifier to your regex
/\b(cum)\b/iu
Difference between \b and \B in regex
The confusion stems from your thinking \b
matches spaces (probably because "b" suggests "blank").
\b
matches the empty string at the beginning or end of a word. \B
matches the empty string not at the beginning or end of a word. The key here is that "-" is not a part of a word. So <left>-<right>
matches \b-\b
because there are word boundaries on either side of the -
. On the other hand for <left> - <right>
(note the spaces), there are not word boundaries on either side of the dash. The word boundaries are one space further left and right.
On the other hand, when searching for \bcat\b
word boundaries behave more intuitively, and it matches " cat " as expected.
PHP word boundary /b regex not working with French
It seems like its a nightmare to fix the ?? display in the French locale, but I was able to fix this problem another way by modifying the regex pattern instead. By adding 'u' as a modifier in the patter it was able to detect the French character ç in ça and all works properly with no need to change the locale.
From this:
$pattern=(\b".$value."\b)
to this:
$pattern=(\b".$value."\b/u)
PHP regex with word boundary after escaped character
The recommended way to solve this is using lookarounds to asserts word characters instead of boundaries, e.g. (?<!\w)c\+\+(?!\w)
:
$string = 'I don\'t know C e C++ so well, but I can code in PHP.';
$languages = [
'PHP' => '/php/',
'C++' => '/cpp/',
'C' => '/c/',
];
foreach ($languages as $name => $uri) {
$regex = '/(?<!\w)' . preg_quote($name, '/') . '(?!\w)/';
if (preg_match($regex, $string)) {
echo "For {$name} information refer to http://foo.bar{$uri}" . PHP_EOL;
}
}
Output:
For PHP information refer to http://foo.bar/php/
For C++ information refer to http://foo.bar/cpp/
For C information refer to http://foo.bar/c/
Regex word boundary alternatives inside parentheses does not work?
This regex is wrong:
preg_match("/[^\d+\-]\bRDS|xyz|ABC\b/i", $input, $output);
Since: [^\d+\-]
means match everything except:
- a digit
- a literal
+
- a literal
-
You can just use:
preg_match("/^\d+\-\b(RDS|xyz|ABC)\b/i", $input, $output);
Related Topics
How to Properly Use Bearer Tokens
Is_Null($X) VS $X === Null in PHP
How to Inject a Repository into a Service in Symfony
Login Only If User Is Active Using Laravel
PHP Date Add 5 Year to Current Date
Adding 30 Minutes to Datetime PHP/Mysql
Apply a Coupon Programmatically in Woocommerce
PHP - Check If the Page Run on Mobile or Desktop Browser
Symfony2 Create Own Encoder for Storing Password
How to Enable --Enable-Soap in PHP on Linux
PHP Constructor to Return a Null
How to Get CSV File to Download on Ie? Works on Firefox
How to Create and Store Md5 Passwords in MySQL
How to Get Open Graph Protocol of a Webpage by PHP