Hi i am having an issue in trying to extract variables using preg replace .I guess i am messing with the delimiters or just doing it wrong
Subject
'file': 'EoWviKqVizQ,end=1384596943/data=B262F941/speed=375k/2305873_hd.flv',
I need to extract
end=1384596943/data=B262F941/speed=375k/1234_hd.flv
This is basically the string after the comma in between the single quotes.
My attempts
preg_match('#'file':'(.*)'#',$input , $matches)
preg_match("#'file':'(.*)'#",$input , $matches)
Hope someone can help me out
Regards
Just do this:
$input = "'file': 'EoWviKqVizQ,end=1384596943/data=B262F941/speed=375k/2305873_hd.flv',";
$mypart = preg_replace("/^'file': '[^,]+,/", "", $input); // strip first part, i.e., "'file': 'EoWviKqVizQ,"
$mypart = preg_replace("/',\s*$/", "", $mypart); // strip last part, i.e., "',"
echo $mypart;
EDITED based on OP feedback (replaced initial [^']+ with file to match only lines starting with 'file', etc.
Related
I'm having some issues with the preg-split function.
I would like to get what is before the delimiter instead of what's after it.
I've found some leads explaining that using the following code would do the trick :
$var = end(preg_split('/\./',$string));
echo($var[0]);
But when I'm doing that I only get the first char and not every chars before the dot.
Here is my code :
$item = "software_technical_item.TI";
$joint = end(preg_split('/\./',$item));
I obviously get "TI" in $joint, I would like to get "software_technical_item", would someone know how to do that ?
Thanks,
Corentin.
Dot is a special character in regex which matches any character , you need to escape it in-order to match a literal dot.
$string = "software_technical_item.TI";
$var = preg_split('/\./',$string);
echo($var[0]);
Output:
software_technical_item
What I want to do is to get the attribute value from a simple text I'm parsing. I want to be able to contain HTML as well inside the quotes, so that's what got me stalling right now.
$line = 'attribute = "<p class=\"qwerty\">Hello World</p>" attribute2 = "value2"'
I've gotten to the point (substring) where I'm getting the value
$line = '"<p class=\"qwerty\">Hello World</p>" attribute2 = "value2"'
My current regex works if there are no escaped quotes inside the text. However, when I try to escape the HTML quotes, it doesn't work at all. Also, using .* is going to the end of the second attribute.
What I'm trying to obtain from the string above is
$result = '<p class=\"qwerty\">Hello World</p>'
This is how far I've gotten with my trial and error regex-ing.
$value_regex = "/^\"(.+?)\"/"
if (preg_match($value_regex, $line, $matches))
$result = $matches[1];
Thank you very much in advance!
You can use negative lookbehind to avoid matching escaped quotes:
(?<!\\)"(.+?)(?<!\\)"
RegEx Demo
Here (?<!\\) is negative lookbehind that will avoid matching \".
However I would caution you on using regex to parse HTML, better to use DOM for that.
PHP Code:
$value_regex = '~(?<!\\\\)"(.+?)(?<!\\\\)"~';
if (preg_match($value_regex, $line, $matches))
$result = $matches[1];
I thought I had this working; however after further evaluation it seems it's not working as I would have hoped it was.
I have a query pulling back a string. The string is a comma separated list just as you see here:
(1,145,154,155,158,304)
Nothing has been added or removed.
I have a function that I thought I could use preg_match to determine if the user's id was contained within the string. However, it appears that my code is looking for any part.
preg_match('/'.$_SESSION['MyUserID'].'/',$datafs['OptFilter_1']))
using the same it would look like such
preg_match('/1/',(1,145,154,155,158,304)) I would think. After testing if my user id is 4 the current code returns true and it shouldn't. What am I doing wrong? As you can see the id length can change.
It's better to have all your IDs in an array then checking if a desired ID is existed:
<?php
$str = "(1,145,154,155,158,304)";
$str = str_replace(array("(", ")"), "", $str);
$arr = explode(',', $str);
if(in_array($_SESSION['MyUserID'], $arr))
{
// ID existed
}
As your string - In dealing with Regular Expressions, however it's not recommended here, below regex will match your ID if it's there:
preg_match("#[,(]$ID[,)]#", $str)
Explanations:
[,(] # a comma , or opening-parenthesis ( character
$ID # your ID
[,)] # a comma , or closing-parenthesis ) character
sorry but i cant solve my problem, you know , Im a noob.
I need to find something in string with preg_match.. then replace it with new word using preg_replace, that's ok, but I don't understand how to put replaced word back to that string.
This is what I got
$text ='zda i "zda"';
preg_match('/"(\w*)"/', $text);
$najit = '/zda/';
$nahradit = 'zda';
$o = '/zda/';
$a = 'if';
$ahoj = preg_replace($najit, $nahradit, $match[1]);
Please, can you help me once again?
You can use e.g. the following code utilizing negative lookarounds to accomplish what you want:
$newtext = preg_replace('/(?<!")zda|zda(?!")/', 'if', $text)
It will replace any occurence of zda which is not enclosed in quotes on both sides (i.e. in U"Vzda"W the zda will be replaced because it is not enclosed directly into quotes).
im doing parsing and the kind of text that i want to match and then make it null is as follows :-
<tr class="label-BGC"><td colspan="4">any kind of text here</td></tr>
i want to match every line that contains "<tr class="label-BGC"><td colspan="4">any text</td></tr>"
its evening here and my brain-battery is totally down
what im trying to do is :-
$patterns='<td colspan="4">'.stristr($parsed,'[^a-z0-9_- $]').'</td></tr>';
$replacements=' ';
$parsed = str_replace($patterns, $replacements, $parsed);
$parsed is containing the whole data that im parsing.
my code is not working can anyone help me with some suggestions here!!!
Try something simple like this:
$parsed = preg_replace('{<tr class="label-BGC"><td colspan="4">.*?</td></tr>}',
$replacements, $parsed);
The . matches any character, the * makes it match 0-many, and the ? stops it being greedy -i.e. it will stop at the first sequence, and not the last possible one.
Try the following:
preg_replace("`\s*<tr\s+class\s*=\s*"label-BGC"\s*>\s*<td\s+colspan\s*=\s*"4"\s*>.*?</td\s*>\s*</tr\s*>\s*`i", "", $content);
All the \s matching might be a little overkill, but it's very forgiving if this input is coming from a user.
Otherwise, you can probably just get away with
preg_replace("`<tr class="label-BGC"><td colspan="4">.*?</td></tr>`i", "", $content);