I currently have bbcode like this
[caption=Some text goes here]image.jpg[/caption]
I'd like to use php's preg_match so I can get the value of the image.jpg, regardless of what's next to 'caption='. Can someone help me out?
Raw regex:
]([^\]]+)[/caption]
preg_match("]([^\]]+)[/caption]", myString, $matches)
image.jpg would be in the first group. $matches[1]
(I'm not certain I escaped it correctly in php).
You can use this regex:
$str = '[caption=Some text goes here]image.jpg[/caption]';
if (preg_match('/^\[[^\]]+]([^[]+)/', $str, $arr))
echo "image: $arr[1]\n";
OUTPUT
image: image.jpg
If you want to match the complete bbcode tag, including the caption, use
preg_match("/\[caption=(.+)\](.+)\[\/caption\]/", $myString, $matches);
This will result in the following $matches array:
Array
(
[0] => [caption=Some text goes here]image.jpg[/caption]
[1] => Some text goes here
[2] => image.jpg
)
RegExp is not magic. PHP already has a pre-made extension library to handle BBCode.
Don't reinvent the wheel, and don't make it hard on yourself.
Related
I am creating a PHP template page that I dont want the end-user to have to write PHP code in. So I would like to set up a system that replaces [[string]] with $string. This way I dont have to maintain a list of variables to look up, I can just use the name within the brackets and if there is a matching variable in the code, it will replace it.
I know how to capture all the double bracketed text.
$text = 'string [[of]] text [[to]] test'
preg_match_all("/\[\[[^\]]*\]\]/", $text, $matches);
I think that preg_match_all returns an array of all the matches, but the only way I can seem to have access into it is something like $matches[1], but this won't work since I dont know how many matches there will be.
I have tried
foreach ($matches as $match) {
///code
}
But I think this foreach is looking at the whole array, not the inside.
Once I get each match I know how to remove the two brackets and turn it into a variable PHP will recognize, instead of a string.
str_replace($match, ${substr($match, 2, -2)}, $text)
If you have a better idea of how to turn double bracketed text in HTML file into variables PHP will read without knowing what is in the double brackets first I would be interested in another solution as well.
preg_match_all('/\[\[.*?\]\]/',$string,$matches);
Will give $matches:
Array
(
[0] => Array
(
[0] => [[of]]
[1] => [[to]]
)
)
So you can loop $matches[0]
I have a problem creating regexpr for capture all IDs from tags imgs. i have a code:
#<span><img (id=\"([^"]*)\").*><\/span>#
Example:
https://regex101.com/r/aN0uO0/3
only capture ID from first tag IMG.
Thanks.
Thats because you are not searching globally, to enable that enter g on the right text field ( after the right / mark ).
If you are using preg_match in php, use preg_match_all and it will work.
If you're eager to use a regex at all, you might get along with:
<img[^>]*?id=(['"])(.+?)\1
You'll find the id in the second group ($2). However, if you have a > somewhere in an attribute (which is totally valid in HTML), the regex won't work as expected. In PHP code this would be:
$regex = '~<img[^>]*?id=([\'"])(.+?)\1~';
$string = 'your_string_here';
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
See an example on regex101.com.
Hint: It's almost always better to use a parser, e.g. SimpleXML or DomDocument.
I have this preg_split function with the pattern to search for any <br>
However, I would like to add some more pattern to it besides <br>.
How can I do that with the current line of code below?
preg_split('/<br[^>]*>/i', $string, 25);
Thanks in advance!
i cant comment thats why im putting an answer,
tell me what you need to be implemented \,
or use a website like PHP live regex creator
PHPs preg_split() function only accepts a single pattern argument, not multiple. So you have to use the power of regular expressions to match your delimiters.
This would be an example:
preg_split('/(<br[^>]*>)|(<p[^>]*>)/i', $string, 25);
If matches on html line breaks and/or paragraph tags.
It is helpful to use a regex tool to test ones expressions. Either a local one or a web based service like https://regex101.com/
The above slips the example text
this is a <br> text
with line breaks <br /> and
stuff like <p>, correct?
like that:
Array
(
[0] => this is a
[1] => text
with line breaks
[2] => and
stuff like
[3] => , correct?
)
Note however that for parsing html markup a DOM parser probably is the better alternative. You don't risk to stumble over escaped characters and the like...
I have a string such as this:
Land of gray [example]here is an example[/example] and pink.
I'm struggling to get the PHP/regex code that will return the contents of the [example] tag into a variable...
I'm not a PHP expert but... this regex will work
\[example\]([^\[]*)\[
That will capture the contents in the capture group.
So your example contents should be in $matches[1] ???
ex:
<?php
$subject = "Land of gray [example]here is an example[/example] and pink.";
$pattern = '/\[example\]([^\[]*)\[/';
preg_match($pattern, $subject, $matches);
print_r($matches[1]);
?>
I didn't test the code above because I don't have PHP running on this machine but I think that would work...
BB code allows attributes. [url="somelink"]click[/url] will not be parsed properly with this regex. I would like to give you an answer, but my regex does'nt work well and it's the reason I got here in the first place. Lol. ;-)
Given a string such as:
a:2:{i:0;s:1:"1";i:1;s:1:"2";}
I want to find every integer within quotes and create an array of all integers found in the string.
End result should be an array like:
Array
(
[0] => 1
[1] => 2
)
I'm guessing you use preg_match() but I have no experience with regular expressions :(
How about this:
$str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
print_r(array_values(unserialize($str)));
Not a regex, same answer.
This works because the string you have is a serialized PHP array. Using a regex would be the wrong way to do this.
The regex (in a program) would look like this:
$str = 'a:2:{i:0;s:1:"1";i:1;s:1:"2";}';
preg_match_all('/"(\d+)"/', $str, $matches);
print_r($matches[1]);