hackalive Posted October 16, 2011 Share Posted October 16, 2011 Hi guys, I have been using this code: preg_match('/<h2>(.*?)<\/h2>/', $data, $matches); which i changed to: $tag1 = 'h2'; $tag2 = 'h2'; preg_match('/$tag1(.*?)$tag2/', $data, $matches); however i need it to work allowing variables (of any character or symbol) within $tag1. I tried $tag1 = 'h(.*?)2'; $tag2 = 'h2'; preg_match('/$tag1(.*?)$tag2/', $data, $matches); but that does not work .... essentiall the whole thing would work like this (example only): firstpartoftag1VARIABLE1lastpartoftag1VARIABLE2tag2 ps the preg_match should only capture the VARIABLE2 data not VARIABLE1 any ideas or help would be much appreciated cheers in advance Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/ Share on other sites More sharing options...
hackalive Posted October 16, 2011 Author Share Posted October 16, 2011 UPDATE: I tried $tag1 = '"h".*?)"2":['; $tag2 = '"h2"}'; preg_match('/$tag1(.*?)$tag2/', $data, $matches); my data set looks like this (don't as why, it just does ) "h":randomrandomrandom"2":[ needtogetthisdataintostrings "h2"} "h":randomrandomrandomo"2":[ needtogetthisdataintostringso "h2"} "h":randomrandomrandomp"2":[ needtogetthisdataintostringsp "h2"} "h":randomrandomrandomq"2":[ needtogetthisdataintostringsq "h2"} so i end up needing an array with the values: needtogetthisdataintostrings needtogetthisdataintostringso needtogetthisdataintostringsp needtogetthisdataintostringsq as I sai before, any and all help much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279716 Share on other sites More sharing options...
codefossa Posted October 16, 2011 Share Posted October 16, 2011 $string = '"h":randomrandomrandom"2":[ needtogetthisdataintostrings "h2"} "h":randomrandomrandomo"2":[ needtogetthisdataintostringso "h2"} "h":randomrandomrandomp"2":[ needtogetthisdataintostringsp "h2"} "h":randomrandomrandomq"2":[ needtogetthisdataintostringsq "h2"}'; preg_match_all('/\:\[\s(.*)\s\"h2\"\}/', $string, $matches); $strings = $matches[1]; foreach ($strings as $str) { echo "{$str}<br />"; } Will Output needtogetthisdataintostrings needtogetthisdataintostringso needtogetthisdataintostringsp needtogetthisdataintostringsq Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279717 Share on other sites More sharing options...
hackalive Posted October 16, 2011 Author Share Posted October 16, 2011 thanks but what happened to the opening h ? I need that as well!! and it needs to use the variables $tag1 and $tag2, please Thanks for you help so far. appreciated Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279718 Share on other sites More sharing options...
codefossa Posted October 16, 2011 Share Posted October 16, 2011 $string = '"h":randomrandomrandom"2":[ needtogetthisdataintostrings "h2"} "h":randomrandomrandomo"2":[ needtogetthisdataintostringso "h2"} "h":randomrandomrandomp"2":[ needtogetthisdataintostringsp "h2"} "h":randomrandomrandomq"2":[ needtogetthisdataintostringsq "h2"}'; preg_match_all('/\"h\"\.*)\"2\"\:\[\s(.*)\s\"h2\"\}/', $string, $matches); list($full, $left, $right) = $matches; foreach ($left as $str) { echo "{$str}<br />"; } echo "<br /><br />"; foreach ($right as $str) { echo "{$str}<br />"; } Outputs randomrandomrandom randomrandomrandomo randomrandomrandomp randomrandomrandomq needtogetthisdataintostrings needtogetthisdataintostringso needtogetthisdataintostringsp needtogetthisdataintostringsq Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279719 Share on other sites More sharing options...
hackalive Posted October 16, 2011 Author Share Posted October 16, 2011 Kira, I don't need randomrandomrandom randomrandomrandomo randomrandomrandomp randomrandomrandomq at all. I need preg_match_all('/\"h\"\.*)\"2\"\:\[\s(.*)\s\"h2\"\}/', $string, $matches); to work with $tag1 and $tag2, please If it does not make sense what I want, let me know Cheers Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279720 Share on other sites More sharing options...
hackalive Posted October 16, 2011 Author Share Posted October 16, 2011 I actually am going to use it with left and right, however .... Kira, I need to use $tag1 and $tag2 not integrated like your demo code. Anyway we can do that? tried $tag2 = '"h2\"'; $pattern = '/\"h\"\.*)\"2\"\:\[\s(.*)\s\$tag2\}/'; but does not work Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279723 Share on other sites More sharing options...
codefossa Posted October 16, 2011 Share Posted October 16, 2011 So is this what you wanted? <?php $string = '"h":randomrandomrandom"2":[ needtogetthisdataintostrings "h2"} "h":randomrandomrandomo"2":[ needtogetthisdataintostringso "h2"} "h":randomrandomrandomp"2":[ needtogetthisdataintostringsp "h2"} "h":randomrandomrandomq"2":[ needtogetthisdataintostringsq "h2"}'; $tag1 = '\"h\"\:'; $tag2 = '\"2\"\:\[\s'; $tag3 = '\s\"h2\"\}'; $pattern = "/{$tag1}(.*){$tag2}(.*){$tag3}/"; preg_match_all($pattern, $string, $matches); list($full, $left, $right) = $matches; foreach ($left as $str) { echo "{$str}<br />"; } echo "<br /><br />"; foreach ($right as $str) { echo "{$str}<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.