Find a string and create a variable with regex - php

I grab the source code of a website with file_get_contents().
Inside this code, i try to detect this king string and put the content of idDM in a variable.
'idDM':'x1mi7f7'
For example, here, $idDM will be equal to x1mi7f7, but the string can be :
'idDM':'xxxxxxx'
And the variable will be xxxxxxx.
I know o have to use REGEX for that. For now, I just manage to find if there is IdDM, but not to recover its contents.
Any advice ? Thanks.

Use the following regex:
'idDM'\s*:\s*'([^']+)'
Explanation:
'idDM' - match the literal string 'idDM' (with the quotes)
\s* - match one or more whitespace character
: - match a literal colon character
\s* - match one or more whitespace character
'([^']+)' - match (and capture) everything that's inside single-quotes
Usage:
$str = "foo bar 'idDM':'x1mi7f7' more baz";
if (preg_match("/'idDM'\s*:\s*'([^']+)'/", $str, $matches)) {
$idDM = $matches[1];
}
var_dump($idDM); // => string(7) "x1mi7f7"
Demo

Related

exploding a search string

I'm trying to make a search string, which can accept a query like this:
$string = 'title -launch category:technology -tag:news -tag:"outer space"$';
Here's a quick explanation of what I want to do:
$ = suffix indicating that the match should be exact
" = double quotes indicate that the multi-word is taken as a single keyword
- = a prefix indicating that the keyword is excluded
Here's my current parser:
$string = preg_replace('/(\w+)\:"(\w+)/', '"${1}:${2}', $string);
$array = str_getcsv($string, ' ');
I was using this above code before, but it doesn't work as intended with the keywords starting on searches like -tag:"outer space". The code above doesn't recognize strings starting with - character and breaks the keyword at the whitespace between the outer and space, despite being enclosed with double quotes.
EDIT: What I'm trying to do with that code is to preg_replace -tag:"outer space" into "-tag:outer space" so that they won't be broken when I pass the string to str_getcsv().
You may use preg_replace like this:
preg_replace('/(-?\w+:)"([^"]+)"/', '"$1$2"', $str);
See the PHP demo online.
The regex matches:
(-?\w+:) - Capturing group 1: an optional - (? matches 1 or 0 occurrences), then 1+ letters/digits/underscores and a :
" - a double quote (it will be removed)
([^"]+) - Capturing group 2: one or more chars other than a double quote
" - a double quote
The replacement pattern is "$1$2": ", capturing group 1 value,
capturing group 2 value, and a ".
See the regex demo here.
Here's how I did it:
$string = preg_replace('/(\-?)(\w+?\:?)"(\w+)/', '"$1$2$3', $string);
$array = str_getcsv($string, ' ');
I considered formats like -"top ten" for quoted multi-word keywords that doesn't have a category/tag + colon prefix.
I'm sorry for being slow, I'm new on regex, php and programming in general and this is also my first post in stackoverflow. I'm trying to learn it as a personal hobby. I'm glad that I learned something new today. I'll be reading more about regex since it looks like it can do a lot of stuff.

Replace middle string by passing specific pattern

I want to replace a middle string by passing the pattern. I have tried it by using pre_replace function. But it is not working for me.
$str = "Lead for Nebhub - Admark
Name: Punam Kalbande
Email: kalbandepunam#gmail.com
Phone Number: 800-703-3209
Nebhub Partner : Nebhub - Admark
Address: PO Box 830395 Miami, FL 33173
Hub : Automotive
Products: ERP, CRM, HCM, Help Desk, Marketing";
$pattern = '/^Hub :(.+)Products:$/i';
$replacement = "Logistics";
$result = preg_replace($pattern, $replacement, $str);
but the above code is only returning original string. It is not replacing with the new one.
The s-Modifier is missing in your Pattern. Further you want to match the Pattern somewhere in the middle of the Text. You used ^, which indicates the Start of the Line and $ which indicates the End of the Line. That means, the whole String must match. Use this Regex, and it will work for you.
/(Hub :)[^\n]+/is
Explanation:
( start Subpattern
Hub the Word Hub
followed by a space
: followed by a Doubledot
) end Subpattern -> accessible by $1 or \1
[^\n]+ match one or more Characters except a Linebreak
i Modifier for caseinsensitive Search
s Modifier to include Linebreaks
What you have to do now is to output the Subpattern in the Replacement too:
$result = preg_replace($pattern, "$1$replacement", $str);

Replace an optional word / expression from a string in php

How can I replace the following optional word/ expression from a given string in php?
Given string - Client Side: ABC Client or Client Side : XYZ Client
Now I want to replace expression from the given word - Client Side: or Client Side : Difference between both of them is that, one word ends with colon $word: and another ends with a extra space and colon $word :
So how can I write a regeX to resolve this problem. ?
I have tried with str_replace, but it would multiple str_replace to do that. I want a clean and simple regeX using preg_replace to replace the given expression.
You can simply use following regex
preg_replace('/\bClient Side\s?:\B/','',$str);
Here I've used \bClient Side\s?:\B regex. Within this regex \s? will check for optional space if yes then also its get matches and if not then also it'll match the given string along with word boundaries
Regex
Demo
You can use preg_replace in the following manner to resolve the problem.
$string = "Client Side: ABC System"; //either this string or 2nd string
$string = "Client SideL : XYZ System";
preg_replace("/Client Site[ ]*[:]/", '', $string)
explanation of parameters used -
/Client Side # Start searching with this word
[ ] # An optional blank space parameter
* # Any number of occurrence of blank space
[:] # Replace colon when it occurs
for brief description of parameters please refer to following URL - http://www.hackingwithphp.com/4/8/6/regular-expression-syntax-examples

Simple HTML Parser

<strong class="tb-rmb-num"><em class="tb-rmb">¥</em>39.00</strong>
I'm trying to retrieve the number only without the currency sign
My current code is
$ret = $html->find('strong[class=tb-rmb-num]');
echo $ret[0];
This will retrieve it with the sign ¥39.00 Advice please, Thank you.
In php:
$string = '¥39.00';
if(preg_match('/([\d\.]+)/', $string, $m)){
echo $m[1];
}
Which outputs:
39.00
Ok, I will break this down:
preg_match('/([\d\.]+)/', $string, $m)
preg_match is a php function that allows us to look for pattern matches in a given string using regular expressions.
The regular expression in this case is: /([\d.]+)/
The / .. / is the delimeter that contains the expression
The ( .. ) is a group, which whatever match is found inside the group is stored in $m. We only have one group (as in only have one set of parenthesis) so this frist group match is retrived via $m[1]
The [ .. ] is a character class.
The \d is a shortcut for all digit characters, 0-9
The . means a literal . character (it is escaped with the \ because a . in a regular expresion has special meaning but we want it to represent a literal . and not it's special meaning)
the + after the character class [..] means match the characters in the class as many times in a row as it can
The value of $string in this example was set to ¥39.00. You will want to replace $string in my example with your $ret[0] instead.
$m is a variable placeholder to store our group matches (explained above)
The whole thing was wrapped in an if statement so you can do something if the pattern match was found else do something else if it wasn't.
For further reference:
http://php.net/manual/en/function.preg-match.php
http://webcheatsheet.com/php/regular_expressions.php

Regular Expression - php - getting spaces not preceded and not followed by a word

Having something like this:
'This or is or some or information or stuff or attention here or testing'
I want to capture all the [spaces] that aren't preceded nor followed by the word or.
I reached this, I think I'm on the right track.
/\s(?<!(\bor\b))\s(?!(\bor\b))/
or this
/(?=\s(?<!(\bor\b))(?=\s(?!(\bor\b))))/
I'm not getting all the spaces, though. What is wrong with this? (the second one was a tryout to get the "and" going")
Try this:
<?php
$str = 'This or is or some or information or stuff or attention is not here or testing';
$matches = null;
preg_match_all('/(?<!\bor\b)[\s]+(?!\bor\b)/', $str, $matches);
var_dump($matches);
?>
How about (?<!or)\s(?!or):
$str='This or is or some or information or stuff or attention here or testing';
echo preg_replace('/(?<!or)\s(?!or)/','+',$str);
>>> This or is or some or information or stuff or attention+here or testing
This uses negitive lookbehind and lookahead, this will replace the space in Tor operator for example so if you want to match only or add trailing and preceding spaces:
$str='Tor operator';
echo preg_replace('/\s(?<!or)\s(?!or)\s/','+',$str);
>>> Tor operator
Code: (PHP Demo) (Pattern Demo)
$string = "You may organize to find or seek a neighbor or a pastor in a harbor or orchard.";
echo preg_replace('~(?<!\bor) (?!or\b)~', '_', $string);
Output:
You_may_organize_to_find or seek_a_neighbor or a_pastor_in_a_harbor or orchard.
Effectively the pattern says:
Match every space IF:
the space is not preceded by the full word "or" (a word that ends in "or" doesn't count), and
the space is not followed by the full word "or" (a word that begins with "or" doesn't count)

Categories