View Full Version : extracting from a pattern
Lionel
09-28-2005, 07:16 PM
I've got this pattern in a category table:
3#35#0:2#15#0:7#93#0:7#101#0:1#1#0:1#3#0:5#71#0:6# 86#0:4#57#0
I must do a query to extract only what is between the # e.g. 35 15 93 101 etc... as subcats
then from those results I need to do another query in another subcategory table in order to get the name from those subcats id
and create a list with a link for each one of them like
a href= view.php?subcat=35>subcat 35 name
a href= view.php?subcat=15>subcat 15 name
etc....
if extracted list count ==1 then instead of displaying a link I simply do a redirect to that subcat (no need to display it for selection)
That one is a big toughy for me. Can someone please help?
Thanks.
Marco van Herwaarden
09-28-2005, 08:43 PM
You can do it with an REGEX, but you should think about normalizing your tables and create new rows for each sub category.
Lionel
09-28-2005, 08:57 PM
That's a premade script from which I am trying to extract that info. I am looking at ereg_replace. That will give me 35 15 93 101 as one line but how do I get them individually as items?
AN-net
09-28-2005, 10:38 PM
explode the spaces and make them into an array
Lionel
09-28-2005, 11:52 PM
That's easy to say :speechless:
I got somewhere the hard way
$string = $f[category];
$string2 = ereg_replace('^.[#]', '', $string);
$string3 = ereg_replace('[#].[:].[#]', ',', $string2);
$string4 = ereg_replace('[#].','', $string3);
$array = array($string4);
before 3#35#0:2#15#0:7#93#0:7#101#0:1#1#0:1#3#0:5#71#0:6# 86#0:4#57#0
after 35,15,93,101,1,3,71,86,57
now I have to find a way to put quotes around the values
then I'll struggle to extract each one of them individually from array
now that my $string5 looks like this
"35" , "15" , "93" , "101" , "1" , "3" , "71" , "86" , "57"
if I do
$array = ("35" , "15" , "93" , "101" , "1" , "3" , "71" , "86" , "57" );
echo count($array);
the count correctly returns 9
but if I do
$array = array($string5);
echo count($array);
I get only one :disappointed:
AN-net
09-29-2005, 12:35 AM
just explode() $string5 using ',' as the separator
Lionel
09-29-2005, 12:40 AM
I did that and I get 35 15 93 101 1 3 71 86 57
but I am still getting 1 for count
$subcats = explode(",", $string5);
$array = array($subcats);
echo "<br />";
echo count($array);
Marco van Herwaarden
09-29-2005, 03:24 AM
There is no need for the line: $array = array($subcats);
Remove that and it will work.
Lionel
09-29-2005, 03:37 AM
:banana: I found a nice article about arrays and I've got everything where I wanted it, only one problem remains and you just gave me perhaps a solution. The count is what is wrong. I have nine items but it is printing 90...
edit: hihihi take that back... the "0" was from an echo I forgot to remove.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.