I've got a string in php it can be in one of two formats - either:
"example1" AND somethingelse = "example2"
or just
"example1"
All I need is to get the data in the speechmarks, whether both or just the one.
Cheers!
preg_match( '/"([^"]*)"/' , $string, $matches )
# $matches is an array with the results
# ignore $matches[0]
# $matches[1] will contain the first string inside double-quotes
# $matches[2] will contain the second string inside double-quotes (if any)
more info here: http://www.php.net/manual/en/function.preg-match.php
If you are looking to replace text, following a certain regex would help:
http://php.net/manual/en/function.preg-replace.php
OR, if you're not wanting to replace text, you can match it using:
http://www.php.net/manual/en/function.preg-match.php
Thanks for the help, I managed it in a slightly backwards way, using the basis of bowsersenior's answer and some simple counting!
//Find amount of Speech marks
$stringCount = substr_count($newstring, '"');
if ($stringCount == 2){
preg_match( '/".*?"/' , $newstring, $matches);
foreach ($matches as $data)
{
print_r($data);
}
}else{
//Get first speech marks
preg_match( '/".*?"/' , $newstring, $matches);
foreach ($matches as $data)
{
print_r($data);
}
//Get second speech marks
$endString = substr($newstring,-4);
echo $endString;
}
Related
I am trying to make this work but I can't:
$str = "<html>1</html><html>2</html><html>3</html>";
$matches = array();
preg_match_all("(?<=<html>)(.*?)(?=</html>)", $str, $matches);
foreach ($matches as $d)
{
echo $d;
}
What I am doing wrong? The output must be:
123
This should work for you:
$str = "<html>1</html><html>2</html><html>3</html>";
preg_match_all("~(?<=<html>).*?(?=</html>)~", $str, $matches);
foreach ($matches[0] as $d) {
echo $d;
}
Output:
123
Changes are:
Use missing regex delimiters ~ in preg_match_all function pattern
Remove capturing group since you are already using lookahead and lookbehind so entire match can be used in further processing
Using $matches[0] in foreach loop instead of $matches
There is no need to declare/initialize $matches before preg_match_all call
You need to add delimiters (I've used !) and you can skip the lookaheads/lookbehinds. Just make a capture group around the numbers you want. Nothing else.
Then look in the second matches element for the array with the individual values.
Example:
preg_match_all("!<html>(.*?)</html>!", $str, $matches);
foreach ($matches[1] as $d)
{
echo $d;
}
I'm trying to get the values of an attribute and fill them in an array:
<p numbers="11.2,1.1 2,3,3.1 33"></p>
preg_match('/numbers="([0-9\\.]]+)"/',$elements,$match);
I need something like this:
$match[0] -> 11.2
$match[1] -> 1.1
$match[2] -> 2
Or anything that would get me to store the numbers in an array, but I can't figure out the regex for it.
Personally I would simply match all numbers and then split them
$matches = preg_match('/numbers="(.*)"/',$elements,$match);
$numbers = explode(',', $matches[1]);
I think I got one solution:
$string = '<tag> etc</tag>
<p numbers="11.2,1.1 2,3,3.1 33"></p>
<div> etc</div>';
// Catch only the numerals from the attribute numbers
preg_match('/<p.*numbers="(.*)"/', $string, $match);
// Catch each number separated by comma or space.
preg_match_all('/(\d+(\.?|\s?)(\d+)?)/', $match[0], $matches);
echo '<pre>';
print_r($matches[0]);
The regex is somehow pretty straightforward, but if you need some clarification, write a comment.
And you can see it tested here: https://3v4l.org/iVp1v
I have a query string stored in a variable and I need to strip out some stuff from it using preg_replace()
the parameters I want to strip out look like this:
&filtered_features[48][]=491
As there will be multiples of these parameters in the query string the 48 and the 491 can be any number so the regex needs to essentially match this:
'&filtered_features[' + Any number + '][]=' + Any number
Anyone know how I would do this?
$string = '&filtered_features[48][]=491';
$string = preg_replace('/\[\d+\]\[\]=\d+/', '[][]=', $string);
echo $string;
I assume you wanted to remove the numbers from the string. This will match a multi-variable query string as well since it just looks for [A_NUMBER][]=A_NUMBER and changes it to [][]=
$query_string = "&filtered_features[48][]=491&filtered_features[49][]=492";
$lines = explode("&", $query_string);
$pattern = "/filtered_features\[([0-9]*)\]\[\]=([0-9]*)/";
foreach($lines as $line)
{
preg_match($pattern, $line, $m);
var_dump($m);
}
/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/'
this will match first number in n1 and second in n2
preg_match_all( '/\&filtered_features\[(?<n1>\d*)\]\[\]\=(?<n2>\d*)/', $str, $matches);
cryptic answer will replace more than necessary with this string:
&something[1][]=123&filtered_features[48][]=491
I have a receipt of a remote page:
$page = file_get_contents ('http://sayt.ru/');
There is a array of words:
$word = array ("word", "second");
How to count the number of words in the array matches the text on the page?
Started to dig in the direction
$matches = array ();
$count_words = preg_match_all ('/'. $word. '/ i',$page, $matches);
But certainly not in the direction I dig because count is always zero. And through preg_match_all sought after one word, not the entire array. : (
you have to either check or each word in array or use regexp like this:
$serachWords = array_map(function($w){ return preg_quote($w,'/'); }, $word);
$search = implode('|', $searchWords);
$count_words = preg_match_all('/\b(?:'.$serach.')\b/i', $page, $matches);
Added few modification to have better results: escape all words, so they wouldn't break expression and add word boundaries (\b) no match word as a word, not part of swords.
I have a lot of strings which have characters like
[anything]
where the word
anything == any kind of characters, such as ~!##$%^&*()
what will be the preg_match() code for matching such string in PHP?
This should do what you need:
<?php
$matches = array();
preg_match_all('/\[(.+?)\]/s', $string, $matches);
foreach ($matches[1] as $match) {
// Do stuff here.
}
That will loop through all the times text between brackets was found in your string.