Jump to content

[SOLVED] Remove


Bakes

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
}

 

 

Link to comment
Share on other sites

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";
}
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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]

Link to comment
Share on other sites

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);

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.