Creating Regular Expression to search a string - php

I have a string that contains variables in the format {namespace:name} I am trying to create a regular expression for finding all of the variables in the string. I have the following so far, but it isn't working:
$str = " {user:fname} and the last name = {user:lname}";
var_dump(preg_match_all("/^\{(\w):(\w)\}/", $str, $matches));
var_dump($matches);
But it isn't finding any of the tags. The variables can have any word for namespace and name, but letters only with no spaces. Any help would be appreciated.
Update
I tried the following also and received no results: "/\{(\w):(\w)\}/"

Remove the anchor ^ from the regex and allow variables with a length of more than one character.
/^\{(\w):(\w)\}/
becomes:
/\{(\w+):(\w+)\}/

Related

Find variable substring in string with PHP

I am about to start working on a project that requires me to do the following:
load the source code of a webpage into a string with file_get_contents
find a certain substring in the first string, that reads "Your code: 6-digit-number-here, with a dash after every 2 digits"
save the first occurrence of the substring into a text file
do the same for each occurrence of the substring in the string
The 6-digit number is different for each occurrence in the source code. How do I define that number in the substring, so I can properly search for it, and how can I make it save every occurrence of the defined substring? Help would be greatly appreciated.
You could use regular expressions to match all codes. In this example the variable $matches would contain all matches from the html string:
$html = file_get_contents('[url]');
preg_match_all('/[0-9]{2}\-[0-9]{2}\-[0-9]{2}/', $html, $matches);
var_dump($matches);
suppose you have variable like
$username = "abcd#somedomain.com";
$username = some
try using this
$username = substr($username, 0, strpos($username, '#'));

Replace string at particular position In PHP?

I have to replace string but it's simple in PHP but my string just like these here i show you.Please any one help me.
$string = "#x93F;#x902;#x91C";
Above string i want to replace it with
#x91C;#x93F;#x902;
But one thing in these string replace. We don't know last word of the $string #x91C; .
Any word comes in last it's place in to front of that string. How can i solve that please any one help me.
Use capturing groups to capture the characters you want. Later you could replace the matched characters with the chars inside the group.
Regex:
^([^;]*);([^;]*);([^;]*);$
Replacement string:
$3;$1;$2;
DEMO
$string = "#x93F;#x902;#x91C;";
echo preg_replace('~^([^;]*);([^;]*);([^;]*);$~', '$3;$1;$2;', $string);
Output:
#x91C;#x93F;#x902;
((?:[^;]+;)*)([^;]+)(?=$)
Replace by $2;$1.
See demo.
http://regex101.com/r/uH3tP3/9

extract text between two words in php

I got the following URL
http://www.amazon.com/LEGO-Ultimate-Building-Set-Pieces/dp/B000NO9GT4/ref=sr_1_1?m=ATVPDKIKX0DER&s=toys-and-games&ie=UTF8&qid=1350518571&sr=1-1&keywords=lego
and I want to extract
B000NO9GT4
that is the asin...to now, I can get search between the string, but not in this way I require. I saw the split functin, I saw the explode. but cant find a way out...also, the urls will be different in length so I cant hardcode the length two..the only thing which make some sense in my mind is to split the string so that
http://www.amazon.com/LEGO-Ultimate-Building-Set-Pieces/dp/
become first part
and
B000NO9GT4/ref=sr_1_1?m=ATVPDKIKX0DER&s=toys-and-games&ie=UTF8&qid=1350518571&sr=1-1&keywords=lego
becomes the 2nd part , from the second part , I should extract B000NO9GT4
in the same way, i would want to get product name LEGO-Ultimate-Building-Set-Pieces from the first part
I am very bad at regex and cant find a way out..
can somebody guide me how I can do it in php?
thanks
This grabs both pieces of information that you are looking to capture:
$url = 'http://www.amazon.com/LEGO-Ultimate-Building-Set-Pieces/dp/B000NO9GT4/ref=sr_1_1?m=ATVPDKIKX0DER&s=toys-and-games&ie=UTF8&qid=1350518571&sr=1-1&keywords=lego';
$path = parse_url($url, PHP_URL_PATH);
if (preg_match('#^/([^/]+)/dp/([^/]+)/#i', $path, $matches)) {
echo "Description = {$matches[1]}<br />"
."ASIN = {$matches[2]}<br />";
}
Output:
Description = LEGO-Ultimate-Building-Set-Pieces
ASIN = B000NO9GT4
Short Explanation:
Any expressions enclosed in ( ) will be saved as a capture group. This is how we get at the data in $matches[1] and $matches[2].
The expression ([^/]+) says to match all characters EXCEPT / so in effect it captures everything in the URL between the two / separators. I use this pattern twice. The [ ] actually defines the character class which was /, the ^ in this case negates it so instead of matching / it matches everything BUT /. Another example is [a-f0-9] which would say to match the characters a,b,c,d,e,f and the numbers 0,1,2,3,4,5,6,7,8,9. [^a-f0-9] would be the opposite.
# is used as the delimiter for the expression
^ following the delimiter means match from the beginning of the string.
See www.regular-expressions.info and PCRE Pattern Syntax for more info on how regexps work.
You can try
$str = "http://www.amazon.com/LEGO-Ultimate-Building-Set-Pieces/dp/B000NO9GT4/ref=sr_1_1?m=ATVPDKIKX0DER&s=toys-and-games&ie=UTF8&qid=1350518571&sr=1-1&keywords=lego" ;
list(,$desc,,$num,) = explode("/",parse_url($str,PHP_URL_PATH));
var_dump($desc,$num);
Output
string 'LEGO-Ultimate-Building-Set-Pieces' (length=33)
string 'B000NO9GT4' (length=10)

How to get the word which is in first quotation marks?

Let's say I have the this text (not to be treated as PHP code):
$this->validation->set('username','username','trim');
$this->validation->set('password','password','trim');
$this->validation->set('password2','password2','trim');
$this->validation->set('name','name','trim');
$this->validation->set('surname','surname','trim');
I want to get the list of first words after set( which is in quotation marks in every line, so the output of previous input must be like this:
username
password
password2
name
surname
I think, it's possible with regular expressions. My question is how can I get the list of the words which is in first quotation marks with PHP?
Lets say the variable $text holds the data from your question.
Let's analyse the regular expression /set\('(.*?)'/:
/ is the delimiter.
set\(' and ' are the strings set(' and ', respectively.
.*? is the least amount of (arbitrary) characters between the two aforementioned strings.1
As a result, this regular expression matches:
$this->validation->set('username','username','trim');
To store all the strings you need in the array $matches[1], we can use the function preg_match_all.
It suffices to call preg_match_all("/set\('(.*?)'/", $text, $matches).
1 See also: Regex Tutorial - Repetition with Star and Plus - Laziness Instead of Greediness
Example code:
$text = <<<EOF
\$this->validation->set('username','username','trim');
\$this->validation->set('password','password','trim');
\$this->validation->set('password2','password2','trim');
\$this->validation->set('name','name','trim');
\$this->validation->set('surname','surname','trim');
EOF;
preg_match_all("/set\('(.*?)'/", $text, $matches);
print_r($matches[1]);
$arr = explode("'","this->validation->set('surname','surname','trim')");
print_r($arr);
not sure why you would want to do something like that, but the above should work

how to extract a portion of a string in php

I am using preg_replace() for some string replacement.
$str = "<aa>Let's find the stuff qwe in between <id>12345</id> these two previous brackets</h>";
$do = preg_match("/qwe(.*)12345/", $str, $matches);
which is working just fine and gives the following result
$match[0]=qwe in between 12345
$match[1]=in between
but I am using same logic to extract from the following string.
<text>
<src><![CDATA[<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="36" COLOR="#999999" LETTERSPACING="0" KERNING="0">r1 text 1 </FONT></P></TEXTFORMAT>]]></src>
<width>45%</width>
<height>12%</height>
<left>30.416666666666668%</left>
<top>3.0416666666666665%</top>
<begin>2s</begin>
<dur>10s</dur>
<transIn>fadeIn</transIn>
<transOut>fadeOut</transOut>
<id>E2159292994B083ACA7ABC7799BBEF3F7198FFA2</id>
</text>
I want to extract the string from
r1text1
to
</id>
The Regular expression I currently Have is:
preg_match('/r1text1(.*)</id\>/', $metadata], $matches);
where $metadata is the above string..
$matches does not return anything....
For some reason...how do i do it?
Thanks in advance
If you want to extract the text, you will probably want to use preg_match. The following might work:
preg_match('#\<P[^\>]*\>\<FONT[^\>]*\>(.*\</id\>)#', $string, $matches)
Whatever gets matched in the parantheses can be found later in the $matches array. In this case everything between a <P> tag followed by a <FONT> tag and </id>, including the latter.
Above regex is untested but might give you a general idea of how to do it. Adapt if your needs are a bit different :)
Even if don't know why you would match the regex on a incomplete XML fragment (starting within a <![CDATA[ and ending right before the closing XML tag </id>, you do have three obvious problems with your regex:
As Amri said: you have to escape the / character in the closing XML tag because you use / as the pattern delimiter. By the way, you don't have to escape the > character. That gives you: '/r1text1(.*)<\/id>/' Alternatively you can change the pattern delimiter to # for example: '#r1text1(.*)</id>#' (I will use the first pattern to further develop the expression).
As Rich Adams already said: the text in your example data is "r1_text_1" (_ is a space character) but you match against '/r1text1(.*)<\/id>/'. You have to include the spaces in your regex or allow for a uncertain number of spaces, such as '/r1(?:\s*)text(?:\s*)1(.*)<\/id>/' (the ?: is the syntax for non-capturing subpatterns)
The . (dot) in your regex does not match newlines by default. You have to add the s (PCRE_DOTALL) pattern modifier to let the . (dot) match against newlines as well: '/r1(?:\s*)text(?:\s*)1(.*)<\/id>/s'
you probably need to parse your string/file and extract the value between the FONT tag. Then insert the value into the id tag
Try googling for php parsing.
try this
preg_match('/r1text1(.*)<\/id\>/', $metadata], $matches);
You are using / as the pattern delimiter but your content has / in . You can use \ as the escape character.
In the sample you have "r1 text 1 ", yet your regular expression has "r1text1". The regular expression doesn't match because there are spaces in the string you are trying to match it against. You should include the spaces in the regular expression.

Categories