sandy1028 Posted April 29, 2009 Share Posted April 29, 2009 How to select the string only between <ite> and </ite> from xml file using regular expression and read all the strings to an array Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/ Share on other sites More sharing options...
Mchl Posted April 29, 2009 Share Posted April 29, 2009 preg_match_all("#<ite>(.*?)(?=</ite>)#",$string,$matches); var_dump($matches[1]); Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-821608 Share on other sites More sharing options...
sandy1028 Posted April 29, 2009 Author Share Posted April 29, 2009 Hi, Can you please tell me what is $string and $matches. I have to use foreach ($data as $dat){ preg_match_all("#<ite>(.*?)(?=</ite>)#",$string,$matches); } This gives a blank result Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-821618 Share on other sites More sharing options...
Mchl Posted April 29, 2009 Share Posted April 29, 2009 preg_match_all Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-821620 Share on other sites More sharing options...
sandy1028 Posted April 29, 2009 Author Share Posted April 29, 2009 here $matches is a two dimensinal array and $matches[1][variable] How to store it in a array and make it one dimensional array foreach ($data as $dat){ preg_match_all("#<ite>(.*?)(?=</ite>)#",$string,$matches); print $matches[1][[b]4[/b]]; } Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-821644 Share on other sites More sharing options...
nrg_alpha Posted April 29, 2009 Share Posted April 29, 2009 When using preg_match_all, it will always be stored into a multidimensional array. So long as you know which part of the array to access, it shouldn't be problematic.. so in this case, if you know you have use what is in $matches[1], then simply utilize that part of the array (ignoring the rest). You could simply use a foreach loop on $matches[1] and store this into a new one dimensional array: preg_match_all("#<ite>(.*?)(?=</ite>)#",$string,$matches); $matchedInfo = array(); // this will be your new one dimensional array foreach($matches[1] as $key => $val){ $matchedInfo[$key] = $val; echo $matchedInfo[$key] . "<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-821973 Share on other sites More sharing options...
sandy1028 Posted April 30, 2009 Author Share Posted April 30, 2009 www.aaa.com/ - 3k How to extract only www.aaa.com using regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-822425 Share on other sites More sharing options...
premiso Posted May 1, 2009 Share Posted May 1, 2009 www.aaa.com/ - 3k How to extract only www.aaa.com using regular expressions. If your output is www.aaa.com/ and you just want www.aaa.com simply use substr... $url = substr($url, 0, (strlen($url) - 1)); Should chop off the last character for you. Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-823473 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 You could simply use a foreach loop on $matches[1] and store this into a new one dimensional array: preg_match_all("#<ite>(.*?)(?=</ite>)#",$string,$matches); $matchedInfo = array(); // this will be your new one dimensional array foreach($matches[1] as $key => $val){ $matchedInfo[$key] = $val; echo $matchedInfo[$key] . "<br />\n"; } Or you could just do $matchedInfo = $matches[1]. That would be easier Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-824915 Share on other sites More sharing options...
nrg_alpha Posted May 3, 2009 Share Posted May 3, 2009 You could simply use a foreach loop on $matches[1] and store this into a new one dimensional array: preg_match_all("#<ite>(.*?)(?=</ite>)#",$string,$matches); $matchedInfo = array(); // this will be your new one dimensional array foreach($matches[1] as $key => $val){ $matchedInfo[$key] = $val; echo $matchedInfo[$key] . "<br />\n"; } Or you could just do $matchedInfo = $matches[1]. That would be easier Indeed, hence: So long as you know which part of the array to access, it shouldn't be problematic.. so in this case, if you know you have use what is in $matches[1], then simply utilize that part of the array (ignoring the rest). I figured the $matches[1] was self explanatory (perhaps not?)... I suggested the alternative in case the OP still wasn't comfortable with dealing with even part of a multi-dimensional array (it wouldn't be the path I would take, as I understand how to access $matches[1].. but yeah, perhaps I should have actually given the $matchedInfo = $matches[1] sample as you have done, and if that was still not comfortable enough for the OP, then move to plan b. My bad. P.S didn't even know about the [tt] tag... Heh... learn something new every day. (tt= tinytext?) Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-824931 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 P.S didn't even know about the [tt] tag... Heh... learn something new every day. (tt= tinytext?) It's short for teletype. It's also a tag in HTML: http://reference.sitepoint.com/html/tt Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-824969 Share on other sites More sharing options...
nrg_alpha Posted May 3, 2009 Share Posted May 3, 2009 Ah, thanks for the heads up. Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-824993 Share on other sites More sharing options...
premiso Posted May 3, 2009 Share Posted May 3, 2009 Ah, thanks for the heads up. I always thought it meant "Teletubbie" so I avoid using it less that purple freak comes out at me from my computer screen. Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-825062 Share on other sites More sharing options...
nrg_alpha Posted May 3, 2009 Share Posted May 3, 2009 Hehe.. yeah, you don't want that purple monster coming at you.. he may look harmless, but given his raw mass (and lack of ability to feel pain because he's always god damn happy), he'll mess you up. I'm looking over at the Simple Machines website on bbc tags (as I really am curious which ones actually exist). If I'm not mistaken, [m]..[/m] is a phpfreaks custom built one (correct?), but is there any additional ones not from SM? P.S Yeah, just saw the tt in the list: [tt]text[/tt] Formats enclosed text in teletype format. I figured it was a custom tag, but could have simply looked it up :-\ Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-825083 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 Yeah the manual tag is one we added. Quote Link to comment https://forums.phpfreaks.com/topic/156072-regular-expression/#findComment-825084 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.