Hi how do I do a preg match on
$string1 = "[%refund%]processed_by"
$string2 = "[%refund%]date_sent"
I want to grab the bits inside %% and then remove the [%item%] altogether. leaving just the "proccessed_by" or "date_sent" I have had a go below but come a bit stuck.
$unprocessedString = "[%refund%]date_sent"
$match = preg_match('/^\[.+\]/', $unprocessedString);
$string = preg_replace('/^\[.+\]/', $unprocessedString);
echo $match; // this should output refund
echo $string; // this should output date_sent
Your problem is with your use of the preg_match function. It returns the number of matches found. But if you pass it a variable as a third parameter, it stores the matches for the entire pattern and its subpatterns in an array.
So you can capture both of the parts you want in subpatterns with preg_match, which means you don't need preg_replace:
$unprocessedString = "[%refund%]date_sent"
preg_match('/^\[%(.+)%\](.+)/', $unprocessedString, $matches);
echo $matches[1]; // outputs 'refund'
echo $matches[2]; // outputs 'date_sent'
Related
According to PHP manual "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."
How can I return a value from a string with only knowing the first few characters?
The string is dynamic and will always change whats inside, but the first four character will always be the same.
For example how could I return "Car" from this string "TmpsCar". The string will always have "Tmps" followed by something else.
From what I understand I can return using something like this
preg_match('/(Tmps+)/', $fieldName, $matches);
echo($matches[1]);
Should return "Car".
Your regex is flawed. Use this:
preg_match('/^Tmps(.+)$/', $fieldName, $matches);
echo($matches[1]);
$matches = []; // Initialize the matches array first
if (preg_match('/^Tmps(.+)/', $fieldName, $matches)) {
// if the regex matched the input string, echo the first captured group
echo($matches[1]);
}
Note that this task could easily be accomplished without regex at all (with better performance): See startsWith() and endsWith() functions in PHP.
"The string will always have "Tmps" followed by something else."
You don't need a regular expression, in that case.
$result = substr($fieldName, 4);
If the first four characters are always the same, just take the portion of the string after that.
An alternative way is using the explode function
$fieldName= "TmpsCar";
$matches = explode("Tmps", $fieldName);
if(isset($matches[1])){
echo $matches[1]; // return "Car"
}
Given that the text you are looking in, contains more than just a string, starting with Tmps, you might look for the \w+ pattern, which matches any "word" char.
This would result in such an regular expression:
/Tmps(\w+)/
and altogether in php
$text = "This TmpsCars is a test";
if (preg_match('/Tmps(\w+)/', $text, $m)) {
echo "Found:" . $m[1]; // this would return Cars
}
I remove the first occurrence of a html tag in a string with preg_replace as
$html = preg_replace('#<p>(.*?)</p>#i', '', $html, 1);
How can I save the deleted substring as a new string:
$deleted = '<p>...</p>';
How about using preg_replace_callback which passes the match to a user defined function.
$str = preg_replace_callback($re, function ($m) use (&$p) { $p = $m[0]; }, $str, 1);
$m any variable name for the matches array where index 0 is the full pattern match
use (&$p) any variable name for storing the match (passed by reference).
See this demo at eval.in
to get the substring you can use preg_match():
preg_match('#<p>(.*?)</p>#i', $html, $matches);
just check the $matches variable it contain what you need.
If matches is provided, then it is filled with the results of search.
$matches[0] will contain the text that matched the full pattern,
$matches1 will have the text that matched the first captured
parenthesized subpattern, and so on.
after that use preg_replace to delete the <p> tag.
but if you want only to remove the <p> tag and keep the substring just preg_replace like this :
$html = "left <p>substring</p> right";
$html = preg_replace('#</?p>#i', '', $html);
echo $html;
output :
left substring right
try using :
$result = preg_match('#<p>(.*?)</p>#i', $html);
I have an array of words and a string and want to add a hashtag to the words in the string that they have a match inside the array. I use this loop to find and replace the words:
foreach($testArray as $tag){
$str = preg_replace("~\b".$tag."~i","#\$0",$str);
}
Problem: lets say I have the word "is" and "isolate" in my array. I will get ##isolate at the output. this means that the word "isolate" is found once for "is" and once for "isolate". And the pattern ignores the fact that "#isoldated" is not starting with "is" anymore and it starts with "#".
I bring an example BUT this is only an example and I don't want to just solve this one but every other possiblity:
$str = "this is isolated is an example of this and that";
$testArray = array('is','isolated','somethingElse');
Output will be:
this #is ##isolated #is an example of this and that
You may build a regex with an alternation group enclosed with word boundaries on both ends and replace all the matches in one pass:
$str = "this is isolated is an example of this and that";
$testArray = array('is','isolated','somethingElse');
echo preg_replace('~\b(?:' . implode('|', $testArray) . ')\b~i', '#$0', $str);
// => this #is #isolated #is an example of this and that
See the PHP demo.
The regex will look like
~\b(?:is|isolated|somethingElse)\b~
See its online demo.
If you want to make your approach work, you might add a negative lookbehind after \b: "~\b(?<!#)".$tag."~i","#\$0". The lookbehind will fail all matches that are preceded with #. See this PHP demo.
A way to do that is to split your string by words and to build a associative array with your original array of words (to avoid the use of in_array):
$str = "this is isolated is an example of this and that";
$testArray = array('is','isolated','somethingElse');
$hash = array_flip(array_map('strtolower', $testArray));
$parts = preg_split('~\b~', $str);
for ($i=1; $i<count($parts); $i+=2) {
$low = strtolower($parts[$i]);
if (isset($hash[$low])) $parts[$i-1] .= '#';
}
$result = implode('', $parts);
echo $result;
This way, your string is processed only once, whatever the number of words in your array.
i am using php Regular expressions but i can't retrieve values that i group using ()
this is my input
<img src="http://www.example.com/image.jpg" title="title" />
i need only src value , this is my regex '"<img src=\"(.*?)\".*?\/>"'
if i can retrieve First group just like java patterns my problem is sloved
preg_match_all('"<img src=\"(.*?)\".*?\/>"', $source, $re);
print_r($re);
and it return full image tag like this <img src="http://www.example.com/image.jpg" title="title" />
To match a single string, preg_match function is enough. You don't need to go for preg_matchall function. If you want to match more number of strings then you could use preg_matchall function. And also first try to match the exact string through the pattern rather than to go for grouping. If it's impossible to match a particular string then go for grouping.
In the below, matching the exact value of src attribute is done.
You could get the value of src in two ways,
1. positive lookbehind
Regex:
(?<=src=\")[^\"]*
PHP code:(Through match_all)
<?php
$string = "<img src=\"http://www.example.com/image.jpg\" title=\"title\" />";
$regex = '~(?<=src=\")[^\"]*~';
preg_match_all($regex, $string, $matches);
print_r($matches);
?>
PHP code:(Through match)
<?php
$string = "<img src=\"http://www.example.com/image.jpg\" title=\"title\" />";
$regex = '~(?<=src=\")[^\"]*~';
if (preg_match($regex, $string, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> http://www.example.com/image.jpg
Explanation:
(?<=src=\") Positive look-behind is used here. So the regex engine puts the match marker just after to the src=".
[^\"]* Now it starts matching any character zero or more times but not of ". When it finds a ", it stops matching characters.
2. Using \K
Regex:
src=\"\K[^\"]*
PHP code (through match)
<?php
$string = "<img src=\"http://www.example.com/image.jpg\" title=\"title\" />";
$regex = '~src=\"\K[^\"]*~';
if (preg_match($regex, $string, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> http://www.example.com/image.jpg
Explanation:
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match.
src=\"\K So it discards the previously matched src=".
[^\"]* Matches any character zero or more times but not of "
You're using preg_match_all so that you need to pass index as well, use print_r($re[1]); to get results.
I Got It Accidently !
we can Code Like this for first Grouped
print_r($re[1]);
I am struggling to get preg_match to return only the image URL and not the entire matched string. Do I need to use preg_replace after or is that getting hacky?
Possibly, would a different syntax get me what I need?
$source = file_get_contents('http://mysite.co.uk');
preg_match_all("'<div id=\"my-div\"><img src=\"(.*?)\" /></div>'", $source, $match);
echo $match[0][0];
If you use echo $match[0][0] you will have all the text.
<div id="my-div"><img src="blabla bla" /></div>
If you write $match[1][0] instead, you will get your subpattern match:
blabla bla
If you're looking for the first instance, you don't need to use preg_match_all():
$source = file_get_contents('http://mysite.co.uk');
if (preg_match('#<div id="my-div"><img src="(.*?)" /></div>#', $source, $match)) {
echo $match[1];
} else {
// no match found
}
Note that this regex will not match across multiple lines.
If you need all matches, then you were on the right track, but you were using index 0 instead of 1, so:
preg_match_all(..., $match);
foreach ($match as $m) {
echo $m[1]; // Use 1 here instead of 0; 1 is the first capture group, where 0 is the entire matched string
}
By default, preg_match_all always returns the fully matched string as the first item (using the ordering type PREG_PATTERN_ORDER).
From the documentation for PREG_PATTERN_ORDER:
Orders results so that $matches[0] is an array of full pattern
matches, $matches[1] is an array of strings matched by the first
parenthesized subpattern, and so on.
If you're looking for the value of a capturing group, check for a value at index 1 and then use the capturing group reference as a subattribute.
E.g., capturing group 1 would be: $matches[1][0]
To change this behavior you can pass a constant to as the third argument, such as PREG_SET_ORDER, which "Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on."