mag1c1an
asked on
Regex Logic
Hi,
Heres a regex that is partly working, can you tell me where I am going wrong?
(I got the regex from EE, i dont know RegEx's)
basically this is supposed to get the category and value from a template file like this:
eg: <!--movies 30-->
two problems,
1) it is only working if the tag is like this:<!--movies 30--> but refuses to work if the tag is like this <!--movies 15_30-->
2) if I have multiple tags of the same like so <!--movies 15--> <!--movies 30--> it takes only one and ignores the second one if its the same category
***** Test.php ****
<?php
$content = join("",file("test.tpl"));
$categories=array("a","b", "textlinks ");
// This gets the tags and put it into matches[]
foreach ($categories as $value)
{
$pattern = "/<!--".$value." (\d+)?-->/";
if (preg_match($pattern,$cont ent) == 1)
preg_match($pattern,$conte nt,$matche s[]);
}
print_r($matches);
exit;
// In case you are interested, this is my str_replace() routine
// $content = str_replace($matches[$inde x][0],$val ue,$conten t);
?>
***test.tpl***
<html><body><pre>
Pictures:
<!--a 3-->
Movies:
<!--b 15-->
Links 1-15
<!--textlinks 0_15-->
Links 15-30
<!--textlinks 16_30--><pre>
</body></html>
************************** *
Thanks,
Mag
Heres a regex that is partly working, can you tell me where I am going wrong?
(I got the regex from EE, i dont know RegEx's)
basically this is supposed to get the category and value from a template file like this:
eg: <!--movies 30-->
two problems,
1) it is only working if the tag is like this:<!--movies 30--> but refuses to work if the tag is like this <!--movies 15_30-->
2) if I have multiple tags of the same like so <!--movies 15--> <!--movies 30--> it takes only one and ignores the second one if its the same category
***** Test.php ****
<?php
$content = join("",file("test.tpl"));
$categories=array("a","b",
// This gets the tags and put it into matches[]
foreach ($categories as $value)
{
$pattern = "/<!--".$value." (\d+)?-->/";
if (preg_match($pattern,$cont
preg_match($pattern,$conte
}
print_r($matches);
exit;
// In case you are interested, this is my str_replace() routine
// $content = str_replace($matches[$inde
?>
***test.tpl***
<html><body><pre>
Pictures:
<!--a 3-->
Movies:
<!--b 15-->
Links 1-15
<!--textlinks 0_15-->
Links 15-30
<!--textlinks 16_30--><pre>
</body></html>
**************************
Thanks,
Mag
ASKER
Hi,
I am getting this error:
Warning: Wrong parameter count for preg_match_all() in c:\phpdev\www\misc-and- testing\test-tpl4.php on line 13
What to do?
Thanks,
Mag
I am getting this error:
Warning: Wrong parameter count for preg_match_all() in c:\phpdev\www\misc-and- testing\test-tpl4.php on line 13
What to do?
Thanks,
Mag
ASKER
Hi,
The first part is working perfectly....its the second part thats the problem.
Thanks,
Mag
The first part is working perfectly....its the second part thats the problem.
Thanks,
Mag
Only replace the second preg_match with preg_match_all!
Check this,
Insert matches according to your requirement
Replace below loop in place of your loop
-------------------------- ---------- ---------- ---------
foreach ($categories as $value){
$pattern = "/<!--".$value." (\d+)_(\d+)?-->|<!--".$val ue." (\d+)?-->/";
if (preg_match($pattern,$cont ent) == 1)
preg_match($pattern,$conte nt,$matche s[]);
}
print_r($matches);
Insert matches according to your requirement
Replace below loop in place of your loop
--------------------------
foreach ($categories as $value){
$pattern = "/<!--".$value." (\d+)_(\d+)?-->|<!--".$val
if (preg_match($pattern,$cont
preg_match($pattern,$conte
}
print_r($matches);
small change
foreach ($categories as $value)
{
$pattern = "/<!--".$value." (\d+)?-->|<!--".$value." (\d+)_(\d+)?-->/";
if (preg_match($pattern,$cont ent) == 1)
preg_match($pattern,$conte nt,$matche s[]);
}
print_r($matches);
foreach ($categories as $value)
{
$pattern = "/<!--".$value." (\d+)?-->|<!--".$value." (\d+)_(\d+)?-->/";
if (preg_match($pattern,$cont
preg_match($pattern,$conte
}
print_r($matches);
Try this
$pattern = "/<!--".$value." ([0-9_]+)?-->/";
if (preg_match($pattern,$cont ent) == 1)
preg_match_all($pattern,$c ontent,$ma tches);
$pattern = "/<!--".$value." ([0-9_]+)?-->/";
if (preg_match($pattern,$cont
preg_match_all($pattern,$c
ASKER
Hi again,
Something is not working and I am really quite confused, another friend suggested I use preg_replace_callback() and i have checked the function here http://se2.php.net/manual/en/function.preg-replace-callback.php but have not idea how it really works, a bit too advanced for me as i still dont know RegExs.
Anyway, I am copying and pasting the whole script below as it might make more sense then and you can help me a bit better:
****** Start code ***
<?php
require "../tgp-settings.php";
$content = join("",file("test.tpl")); // check template.tpl path
$categories=array("1","2", "3","textl inks");
foreach ($categories as $value)
{
$pattern = "/{".$value." ([0-9_]+)?}/";
// if (preg_match($pattern,$cont ent) == 1)
preg_match($pattern,$conte nt,$matche s[]);
}
$index=0;
foreach ($matches as $key =>$value){
preg_match("/\w+/",$value[ 0][0],$res );
if($res[0] != "textlinks")
{
$query = "select client_id,category,gallery _url,descr iption,thu mb_name,ha s_thumb from tgp_members where category_number='".$res[0] ."' limit ".$value[1]."\n";
$value ="";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
{
$client_id = $row[0];
$category = $row[1];
$gallery_url = $row[2];
$description = $row[3];
$thumb_name = $row[4];
$has_thumb = $row[5];
$value .= $client_id." ".$category." ".$gallery_url." ".$description." ".$thumb_name." ".$has_thumb."<br><br>\n\n ";
}
}
else
{
// echo "in here";
$limit=explode("_",$value[ 1]);
$query = "select gallery_url,description from tgp_members limit ".$limit[0].",".$limit[1];
$value ="";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
{
$gallery_url = $row[0];
$description = $row[1];
$value .= "<a href='".$gallery_url."'>". $descripti on."</a><b r>\n\n";
}
}
$content = str_replace($matches[$inde x][0],$val ue,$conten t);
$index++;
}
echo "<br>".$content;
?>
****** End code *****
Thanks,
Mag
Something is not working and I am really quite confused, another friend suggested I use preg_replace_callback() and i have checked the function here http://se2.php.net/manual/en/function.preg-replace-callback.php but have not idea how it really works, a bit too advanced for me as i still dont know RegExs.
Anyway, I am copying and pasting the whole script below as it might make more sense then and you can help me a bit better:
****** Start code ***
<?php
require "../tgp-settings.php";
$content = join("",file("test.tpl"));
$categories=array("1","2",
foreach ($categories as $value)
{
$pattern = "/{".$value." ([0-9_]+)?}/";
// if (preg_match($pattern,$cont
preg_match($pattern,$conte
}
$index=0;
foreach ($matches as $key =>$value){
preg_match("/\w+/",$value[
if($res[0] != "textlinks")
{
$query = "select client_id,category,gallery
$value ="";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
{
$client_id = $row[0];
$category = $row[1];
$gallery_url = $row[2];
$description = $row[3];
$thumb_name = $row[4];
$has_thumb = $row[5];
$value .= $client_id." ".$category." ".$gallery_url." ".$description." ".$thumb_name." ".$has_thumb."<br><br>\n\n
}
}
else
{
// echo "in here";
$limit=explode("_",$value[
$query = "select gallery_url,description from tgp_members limit ".$limit[0].",".$limit[1];
$value ="";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result))
{
$gallery_url = $row[0];
$description = $row[1];
$value .= "<a href='".$gallery_url."'>".
}
}
$content = str_replace($matches[$inde
$index++;
}
echo "<br>".$content;
?>
****** End code *****
Thanks,
Mag
ASKER
I have changed the template file from <!--movies 12_30--> to this {movies 12_30} so it displays when looking at plain html and also it was giving some small problems in DreamWeaver...
heres my template file:
<html>
<body>
And here are some cartoons:<br>
{2 3} <br><br>
Or if you prefer full movies:
<br><b>{1 15} </b><br><br>
Third and final one:
<br><i>{3 3} </i><br><br><br>
<b>And here come the links 1-15</b>
<br>{textlinks 0_15}
<b>And here come the links 15-30</b>
<br>{textlinks 16_30}
</body>
</html>
heres my template file:
<html>
<body>
And here are some cartoons:<br>
{2 3} <br><br>
Or if you prefer full movies:
<br><b>{1 15} </b><br><br>
Third and final one:
<br><i>{3 3} </i><br><br><br>
<b>And here come the links 1-15</b>
<br>{textlinks 0_15}
<b>And here come the links 15-30</b>
<br>{textlinks 16_30}
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$pattern = "/<!--".$value." (\d+)?-->/";
the \d stands for any number (0-9) and does not include the _. To also include the _ use
[0-9_]+
$pattern = "/<!--".$value." ([0-9_]+)?-->/";
2) to get all those tag use preg_match_all instead of preg_match