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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.