how to use regex special characters in pattern in preg_replace - php

I am trying to replace 2.0 to stack,
but the following code replace 2008 to 2.08
Following is my code:
$string = 'The story is inspired by the Operation Batla House that took place in 2008 ';
$tag = '2.0';
$pattern = '/(\s|^)'.($tag).'(?=[^a-z^A-Z])/i';
echo preg_replace($pattern, '2.0', $string);

Use preg_quote and make sure you pass the regex delimiter as the second argument:
$string = 'The story is inspired by the Operation Batla House that took place in 2008 ';
$tag = '2.0';
$pattern = '/(\s|^)' . preg_quote($tag, '/') . '(?=[^a-zA-Z])/i';
// ^^^^^^^^^^^^^^^^^^^^^
echo preg_replace($pattern, '2.0', $string);
The string is not modified. See the PHP demo. The regex delimiter here is /, thus it is passed as the 2nd parameter to preg_quote.
Note that [^a-z^A-Z] matches any chars but ASCII letters and ^ since you added the second ^ in the character class. I changed [^a-z^A-Z] to [^a-zA-Z].
Also, the capturing group at the start may be replaced with a single lookbehind, (?<!\S), it will make sure your match occurs only at the string start or after a whitespace.
If you expect to also match at the end of the string, replace (?=[^a-zA-Z]) (that requires a char other than a letter immediately to the right of the current location) with (?![a-zA-Z]) (that requires a char other than a letter or end of string immediately to the right of the current location).
So, use
$pattern = '/(?<!\S)' . preg_quote($tag, '/') . '(?![a-zA-Z])/i';
Also, consider using unambiguous word boundaries
$pattern = '/(?<!\w)' . preg_quote($tag, '/') . '(?!\w)/i';

Related

PHP Regular Expression Exclusion

Here is the sample PHP code:
<?php
$str = '10,000.1 $100,000.1';
$pattern = '/(?!\$)\d+(,\d{3})*\.?\d*/';
$replacement_str = 'Without$sign';
echo preg_replace($pattern, $replacement_str, $str);?>
Target is to replace numbers only (i.e. "$100,000.1" should not be replaced). But the above code replaces both 10,000.1 and $100,000.1. How to achieve the exclusion?
This assertion is always true (?!\$)\d+ as you match a digit which can not be a $
As the . and the digits at the end of the pattern are optional, it could also match ending on a dot like for example 0,000.
Instead you can assert a whitespace boundary to the left, and optionally match a dot followed by 1 or more digits:
(?<!\S)\d+(?:,\d{3})*(?:\.\d+)?\b
Regex demo
Example:
$str = '10,000.1 $100,000.1';
$pattern = '/(?<!\S)\d+(?:,\d{3})*(?:\.\d+)?\b/';
$replacement_str = 'Without$sign';
echo preg_replace($pattern, $replacement_str, $str);
Output (If you remove the numbers, the text "Without$sign" is not correct)
Without$sign $100,000.1

Replace all occurrences using preg_replace

The code below works perfectly:
$string = '(test1)';
$new = preg_replace('/^\(+.+\)+$/','word',$string);
echo $new;
Output:
word
If the code is this:
$string = '(test1) (test2) (test3)';
How to generate output:
word word word?
Why my regex do not work ?
^ and $ are anchors which means match should start from start of string and expand upto end of string
. means match anything except newline, + means one or more, by default regex is greedy in nature so it tries to match as much as possible where as we want to match ( ) so we need to change the pattern a bit
You can use
\([^)]+\)
$string = '(test1) (test2) (test3)';
$new = preg_replace('/\([^)]+\)/','word',$string);
echo $new;
Regex Demo

my regex is not working for maximum limit

Hello i have a regex which accepts mostly every character including specials.And i have set
it to accept minimum 8 and maximum 30 characters.
Everything is right for minimum but it's not working for maximum.
If string is more than 30 or any length. The result is true.
The pattern is here:
$pattern = '/[A-Za-z0-9' . preg_quote( '.%^&()$##!/-+/', '/') . ']{8,30}/';
The whole testing code is:
$pattern = '/^[A-Za-z0-9' . preg_quote( '.%^&()$##!/-+/', '/') . ']{8,30}$/';
if(preg_match($pattern, $pass))
{
echo '<br>true';
}
else
{
echo '<br>false';
}
?>
This will match any string up to 30 characters within the string. You need to include the start and end of the string:
$pattern = '/^[A-Za-z0-9' . preg_quote( '.%^&()$##!/-+/', '/') . ']{8,30}$/';
The first $pattern expression in your question is missing the required: ^ and $ beginning and end of line assertions - (but the example code snippet which follows uses them correctly.)
You also need to escape the dash/hyphen inside the character class - the hyphen defines a range of characters. (Note that the forward slash / is NOT the escape char!) Try this:
$pattern = '/^[A-Za-z0-9.%^&()$##!\-+\/]{8,30}$/';

PHP preg_match find certain word

I am trying to use preg_match to find a certain word in a string of text.
$pattern = "/" . $myword . "/i";
This pattern will find the word "car" inside "cartoon"...
I need just matches where the certain word appears.
P.S The word may be anywhere inside the text.
Thanks
Wrap your regex with word-boundaries:
$pattern = "/\b" . $myword . "\b/i";
or, if your $myword may contain regex-meta-chars, do:
$pattern = "/\b" . preg_quote($myword) . "\b/i";
Try this:
$pattern = "/\b" . $myword . "\b/i";
In regular expressions, the \b escape character represents a "word boundary" character. By wrapping your search term within these boundary matches, you ensure that you will only match the word itself.
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREenter code hereG_OFFSET_CAPTURE, 3);
print_r($matches);
pattern
The pattern to search for, as a string.
subject
The input string.
matches
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
flags
flags can be the following flag:
PREG_OFFSET_CAPTURE
If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
offset
Normally, the search starts from the beginning of the subject string. The optional parameter offset can be used to specify the alternate place from which to start the search (in bytes).
Example:
if (preg_match('/;/', $_POST['value_code']))
{
$input_error = 1;
display_error(_("The semicolon can not be used in the value code."));
set_focus('value_code');
}

Attempting to understand handling regular expressions with php

I am trying to make sense of handling regular expression with php. So far my code is:
PHP code:
$string = "This is a 1 2 3 test.";
$pattern = '/^[a-zA-Z0-9\. ]$/';
$match = preg_match($pattern, $string);
echo "string: " . $string . " regex response: " , $match;
Why is $match always returning 0 when I think it should be returning a 1?
[a-zA-Z0-9\. ] means one character which is alphanumeric or "." or " ". You will want to repeat this pattern:
$pattern = '/^[a-zA-Z0-9. ]+$/';
^
"one or more"
Note: you don't need to escape . inside a character group.
Here's what you're pattern is saying:
'/: Start the expressions
^: Beginning of the string
[a-zA-Z0-9\. ]: Any one alphanumeric character, period or space (you should actually be using \s for spaces if your intention is to match any whitespace character).
$: End of the string
/': End the expression
So, an example of a string that would yield a match result is:
$string = 'a'
Of other note, if you're actually trying to get the matches from the result, you'll want to use the third parameter of preg_match:
$numResults = preg_match($pattern, $string, $matches);
You need a quantifier on the end of your character class, such as +, which means match 1 or more times.
Ideone.

Categories