Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php get string between tags [closed]

Tags:

regex

php

I got a string like this which is (Joomla all video plugin)

{Vimeo}123456789{/Vimeo} 

where 123456789 is variable, how can I extract this? Should I use regex?

like image 672
asdf23e32 Avatar asked Sep 12 '14 21:09

asdf23e32


1 Answers

If you must use a regular expression, the following will do the trick.

$str = 'foo {Vimeo}123456789{/Vimeo} bar';
preg_match('~{Vimeo}([^{]*){/Vimeo}~i', $str, $match);
var_dump($match[1]); // string(9) "123456789"

This may be more than what you want to go through, but here is a way to avoid regex.

$str = 'foo {Vimeo}123456789{/Vimeo} bar';
$m = substr($str, strpos($str, '{Vimeo}')+7);
$m = substr($m, 0, strpos($m, '{/Vimeo}'));
var_dump($m); // string(9) "123456789"
like image 112
hwnd Avatar answered Oct 06 '22 00:10

hwnd