Jump to content

preg_match


hackalive

Recommended Posts

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 :D

Link to comment
https://forums.phpfreaks.com/topic/249203-preg_match/
Share on other sites

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 :P)

"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

Link to comment
https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279716
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279717
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279719
Share on other sites

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 :D

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279720
Share on other sites

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 />";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/249203-preg_match/#findComment-1279725
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.