How get list of 'parameters' from string using regular expressions? - php

I have the string with parameters like
"Hello, %{user}. Please, give %{client} a knife".
So, please, I'm not familiar with regular expressions, and I need to have php construction which can get list of parameters from a string. Please, help me, I hope you can. Thank you.

$str = "Hello, %{user}. Please, give %{client} a knife".
preg_match_all("/%\{(.*?)\}/", $str, $matches);
print_r($matches);

Related

Regular expression to match a sample shortcode

I need help with a PHP regular expression that would match the sample shortcode below:
[smiley set_name="Happy" filename="smiling.gif"]
I'd like to extract "Happy" and "smiling.gif" from the above shortcode. I would highly appreciate your help. Thanks in advance.
I'm not sure how much of your string you can assume to know but maybe something like:
/\[smiley set_name="([\w\s]+)" filename="(\w+.gif|jpg|png|jpeg)"\]/
So your code may look like:
$string = '[smiley set_name="Happy" filename="smiling.gif"]';
preg_match('/\[smiley set_name="([\w\s]+)" filename="(\w+.gif|jpg|png|jpeg)"\]/', $string, $matches);
var_dump($matches);

Remove all instances from string

I have a string:
[startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] man this is good!!!
I would like remove everything in between [startstring] and [endstring], all the matches, and also remove the [startstring] and [endstring] from the string. So like the result would be:
hello guys man this is good!!!
and the deleted stuff would be:
[startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] man this is good!!!
See what I mean?
I would like to echo the resulted stuff, without the [startstring] and [endstring] stuff, as shown above. :)
Hope I'm mostly clear. :-)
How would I go about accomplishing this in PHP? Would this envolve some sort of RegEx? Could you provide a sample code?
Thanks so much in advance! :-)
Edit: Could this code be modified or be used somewhat to complete the task?
preg_match_all('/\[startstring\](.*?)\[endstring\]/s', $input, $matches);
To achieve that you just need to change your method name, you should use preg_replace instead preg_match_all:
preg_replace ($pattern, $replacement, $subject)
Searches subject for matches to pattern and replaces them with replacement.
Code
$input = "[startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] man this is good!!!";
$output = preg_replace('/\[startstring\](.*?)\[endstring\]/s', '', $input);
Output
hello guys man this is good!!!

Php find string with regex

I've read multiple tutorials on regex but it just won't stick in my head. I can never get my patterns to work. Hope someone can help.
I have a php variable ($content) where I need to find a certain pattern that looks like this
[gallery::name/of/the/folder/]
I would like to search:
- starting with "[gallery::"
- any other character (variable length)
- ending with "]"
So far, in PHP I have:
preg_match('/\[gallery\:/', $content, $matches, PREG_OFFSET_CAPTURE);
I can find [gallery: but that's it.
I would like to be able to find the rest (:name/of/the/folder/])
Any help is appreciated!
Thanks!
Try capturing it:
preg_match("/\[gallery::(.*?)]/", $content, $m);
Now $m is an array:
0 => [gallery::/name/of/the/folder/]
1 => /name/of/the/folder/
change your regex to
'/\[gallery::([A-Za-z\/]+)\]/'
Since I put the folder/path part in parenthesis, you should get a capture group out of it.

Regular expression - need to get string from html comment

I need to get string from comment in HTML file, I was trying to do it with DOM, but I didn't find good solution with this method.
So I want to try it with regular expressions, but I can't find satisfactory solution. Please, can you help me?
This is what I need:
<!--adress-"String here I need to get"-->
Thanks in advance for answer
Look into $matches after this code
preg_match('~<!--adress-"(.*?)"-->~msi', $string, $matches);
HTML comments are regular; you can just match <!--adress-"([^">]+)"--> and get the first group.
This assumes that the comments are always well-formed and always have a quoted string containing no quotes.
It will be more accurate:
$regex = '<!--(.+?)-"{0,1}(.+?)"{0,1}-->';
preg_match_all($regex, $html, $matches_array);
Just do the var_dump($matches_array) and see results.

Regular expressions PHP preg_match_all

Hi I am trying to use preg_match_all() to extract the number in bold out of an image URL...
http://profile.ak.fbcdn.net/hprofile-ak-snc4/174844_39677118233_8277870_t.jpg
Could someone please help me with the regular expression needed as I am stumped.
I've used this so far:
preg_match_all("(http://profile.ak.fbcdn.net/hprofile-ak-snc4/.*_t.jpg)siU", $this->html, $matching_data);
return $matching_data[0];
}
Which is just giving me an array of the full links.
Hope someone can help, thanks!!!
This will give you all occurrences:
$matches = preg_match_all ('!/hprofile-ak-snc4/[0-9]+_([0-9]+)[^/]+?\.jpg!i', $txt);
print_r ($matches);
Number you have bolded should be contained in $matches[$n][3]...
preg_match_all("#http://profile\.ak\.fbcdn\.net/(.*?)/([0-9]+)_([0-9]+)_([0-9]+)_t\.jpg#is", $string, $matches);
print_r($matches);
Try this:
([a-z][a-z0-9+\-.]*:(//[^/?#]+)?)?
([a-z0-9\-._~%!$&'()*+,;=:#/]*)
(?:(?:\d+_)(\d+)(?:_\d+))\3
I've separated it out onto multiple lines for easier reading. You will want to use capture group 4
Or (just minimized it a bit)
(?:[a-z][a-z0-9+\-.]*:(?://[^/?#]+)?)?
([a-z0-9\-._~%!$&'()*+,;=:#/]*)
(?:(?:\d+_)(\d+)(?:_\d+))\1
and use capture group 2

Categories