Bakes Posted May 28, 2009 Share Posted May 28, 2009 I'm really confused with my REGEX's. They work horribly, so I'm not even going to post them here. 000001 "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:22:06] How would I strip all the characters other than: 671e78a6308032bed8b1d14587e24de4 Please help, I've been stuck with this for hours! Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/ Share on other sites More sharing options...
nrg_alpha Posted May 28, 2009 Share Posted May 28, 2009 Something like this? $str = '000001 "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:22:06]'; preg_match('#GUID=([^(]+)#', $str, $match); echo $match[1]; Granted, this doesn't strip out the characters, but rather fetches just what you are looking for. If you need to strip them out, you can do something like this instead: $str = preg_replace('#^.+?GUID=([^(]+).*#', '$1', $str); This assumes of course that the string is on a line all by itself. Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844406 Share on other sites More sharing options...
Bakes Posted May 28, 2009 Author Share Posted May 28, 2009 I'm trying to use this to get 00001 (It's a url, actual code is): <a href="pb000001.htm" target="_blank">000001</a> "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:22:06] This is what I'm using, it works in testers, yet doesn't in my code. Can you tell what the problem is? preg_match('#<a href="([^"]+)#', $lines[$i], $id); Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844436 Share on other sites More sharing options...
nrg_alpha Posted May 28, 2009 Share Posted May 28, 2009 I'm confused. First you state you want to value of GUID (more or less), and now you want the value of href? You're not being clear on what exactly you are trying to shoot for. If you are looking for pb000001.htm in you string (in other words, the value of your href), add the closing quote after the capture: preg_match('#<a href="([^"]+)"#', $lines[$i], $id); // note the closing " You'll want to echo / use $id[1] for this captured value. Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844443 Share on other sites More sharing options...
Bakes Posted May 28, 2009 Author Share Posted May 28, 2009 I want them both (in seperate values) Unfortunately, that doesn't work (returns a blank page). Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844451 Share on other sites More sharing options...
nrg_alpha Posted May 28, 2009 Share Posted May 28, 2009 I want them both (in seperate values) Why didn't you say so in your initial post? Would make things a little easier, no? So is this something along the lines of what you are looking for? $str = '<a href="pb000001.htm" target="_blank">000001</a> "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:22:06]'; preg_match('#<a href="([^"]+)".+?GUID=([^(]+)#', $str, $id); echo $id[1] . ' - ' . $id[2]; If there is going to be multiple examples of this anchor tag in the string you are searching, you can use preg_match_all instead: $str = '<a href="pb000001.htm" target="_blank">000001</a> "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:22:06] <a href="pb000002.htm" target="_blank">000001</a> "Go Away" (W) GUID=431e78a6308032bed7b1d16679e24de5(VALID) [2009.05.28 19:22:06]'; preg_match_all('#<a href="([^"]+)".+?GUID=([^(]+)#', $str, $id); $count = count($id[0]); for ($a = 0 ; $a < $count ; $a++) { echo $id[1][$a] . ' ' . $id[2][$a] . "<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844463 Share on other sites More sharing options...
Bakes Posted May 28, 2009 Author Share Posted May 28, 2009 Unfortunately, it's still a blank page. My code is: <?php $str = implode('', file('pbsvss.htm')); preg_match_all('#<a href="([^"]+)".+?GUID=([^(]+)#', $str, $id); $count = count($id[0]); for ($a = 0 ; $a < $count ; $a++) { echo $id[1][$a] . ' ' . $id[2][$a] . "<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844513 Share on other sites More sharing options...
nrg_alpha Posted May 28, 2009 Share Posted May 28, 2009 What is the value of $str if you echo it out after imploding? Troublehsooting variables by examining their contents will help out. You can use print_r or var_dump to check out what the variables in question hold. Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844520 Share on other sites More sharing options...
.josh Posted May 28, 2009 Share Posted May 28, 2009 okay first off, instead of this: $str = implode('', file('pbsvss.htm')); do this: $str = file_get_contents('pbsvss.htm'); And then in your regex, do this: preg_match_all('#<a href="([^"]+)".+?GUID=([^(]+)#is', $str, $id); if that doesn't fix it, post some actual content of pbsvss.htm Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844521 Share on other sites More sharing options...
Bakes Posted May 28, 2009 Author Share Posted May 28, 2009 <?php $str = file_get_contents('pbsvss.htm'); preg_match_all('#<a href="([^"]+)".+?GUID=([^(]+)#is', $str, $id); $count = count($id[0]); for ($a = 0 ; $a < $count ; $a++) { echo $id[1][$a] . ' ' . $id[2][$a] . "<br />\n"; } ?> <p> <a href=pb000001.htm target=_blank>000001</a> "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:22:06] <p> <a href=pb000002.htm target=_blank>000002</a> "Bakes" (W) GUID=d990f5eab60849cce1f9a73891f45ece(VALID) [2009.05.28 19:22:19] <p> <a href=pb000003.htm target=_blank>000003</a> "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:22:39] <p> <a href=pb000004.htm target=_blank>000004</a> "Bakes" (W) GUID=d990f5eab60849cce1f9a73891f45ece(VALID) [2009.05.28 19:22:49] <p> <a href=pb000005.htm target=_blank>000005</a> "Go Away" (W) GUID=671e78a6308032bed8b1d14587e24de4(VALID) [2009.05.28 19:23:10] <p> <a href=pb000006.htm target=_blank>000006</a> "Bakes" (W) GUID=d990f5eab60849cce1f9a73891f45ece(VALID) [2009.05.28 19:23:25] Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844540 Share on other sites More sharing options...
.josh Posted May 28, 2009 Share Posted May 28, 2009 okay so the previous content you showed had quotes around the href value. This content you are showing right now does not. I sure hope there isn't any more "confusion" about what you're *really* after... preg_match_all('~href=([^\s]*).+?GUID=([^(]*)~is',$string,$matches); echo "<pre>"; print_r($matches); Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844542 Share on other sites More sharing options...
nrg_alpha Posted May 28, 2009 Share Posted May 28, 2009 Ah, I missed that he was trying to implode a file... Quote Link to comment https://forums.phpfreaks.com/topic/160058-solved-remove/#findComment-844577 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.