JChilds Posted June 1, 2008 Share Posted June 1, 2008 I posted a thread last night, But found myself more confused than where I started, So I have decided to start from the very start again in the direction that your guys suggest. I have the source code to a page. This is a snippet from it Quote <tr> <td><input name="id_1523023" type="checkbox" /><a href="index.php?view=1523023"> <span id="label[1523023]"><img src="graphic/dots/blue.png" title="" alt="" /> <span id="labelText[1523023]">test1</span></a> <td><input name="id_1522728" type="checkbox" /><a href="index.php?view=1522728"> <span id="label[1522728]"><img src="graphic/dots/blue.png" title="" alt="" /> <span id="labelText[1522728]">test2</span></a> I want to extract the bolded parts. They are the only text on the screen that is separated by ID_ and " And, just to make things difficult, I would love for the output to display in the opposite order of what it was found. In this example it would display: 1522728 1523023 Can anyone please point me in the right direction? Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/ Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 good old fashioned regex. moving... Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555208 Share on other sites More sharing options...
haku Posted June 1, 2008 Share Posted June 1, 2008 Actually I don't believe this requires regex at all: post to come... Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555209 Share on other sites More sharing options...
haku Posted June 1, 2008 Share Posted June 1, 2008 $raw_keys = array_keys($_POST); foreach($raw_keys as $raw_key) { $keys[] = str_replace("id_", "", $raw_key); } $keys = array_reverse($keys); note: You will run into troubles if you have any other $_POST values set, as this action will be performed on all of them. You will need to add an if statement into the foreach statement to check that this action should be performed on the array key in question. Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555211 Share on other sites More sharing options...
JChilds Posted June 1, 2008 Author Share Posted June 1, 2008 Quote $raw_keys = array_keys($_POST); foreach($raw_keys as $raw_key) { $keys[] = str_replace("id_", "", $raw_key); } $keys = array_reverse($keys); note: You will run into troubles if you have any other $_POST values set, as this action will be performed on all of them. You will need to add an if statement into the foreach statement to check that this action should be performed on the array key in question. Thanks for the reply. So this is what I have now. <?php $raw_keys = array_keys($_POST); foreach($raw_keys as $raw_key) { $keys[] = str_replace("id_", "", $raw_key); } $keys = array_reverse($keys); echo $keys; echo mysql_error(); ?> It just returns a blank screen with 'Array' on it. here is the input page http://www.noexcuse.com.au/test.html Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555215 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 haku your script depends on him being in control of that form and then parsing the posted vars. That makes no sense because if he had control over the form, he wouldn't need to be parsing it in the first place. He's probably doing a file_get_contents or fopen or something. Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555223 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 That's because $keys is an array. You can't echo out an array like that, its not like echoing out a variable. if you want to see what is inside 'array', you will have to use another foreach loop: foreach ($keys as $key) { echo $key . "<br />"; } But I think you will end up with a screen that says 'id', seeing as the names of your inputs in your example folllowed the pattern of 'id_*******' where the asterisks were some integer, but on the test page you showed, your input just had a name of 'id'. Crayon Violent - that makes sense. Was this the case Jchilds? Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555226 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Quote That's because $keys is an array. You can't echo out an array like that, its not like echoing out a variable. if you want to see what is inside 'array', you will have to use another foreach loop: foreach ($keys as $key) { echo $key . "<br />"; } But I think you will end up with a screen that says 'id', seeing as the names of your inputs in your example folllowed the pattern of 'id_*******' where the asterisks were some integer, but on the test page you showed, your input just had a name of 'id'. Crayon Violent - that makes sense. Was this the case Jchilds? Sorry, I should have clarified. The quote in my first post is what I will be pasting into the textfield on www.noexcuse.com.au/test.html Does this make sense? Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555229 Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 In this case, Crayon Violent was right, and it definitely will require regex. As such I will let someone else who is better with regex help you with that solution. Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555232 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Quote In this case, Crayon Violent was right, and it definitely will require regex. As such I will let someone else who is better with regex help you with that solution. Ok. Sorry for wasting your time. Thanks for teaching me something none the less Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555233 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 I'm just curious: if you are going to manually cut and paste a big chunk of text...why not just manually cut and paste the numbers instead? Or is that example link just a testing ground for pulling a file from somewhere else? I suck at regex too, otherwise I would have given you an answer by now. I mean, I understand the concept, but there are just like, a ton of things to memorize and I'm lazy. I don't really have nothin' better to do, so I'll try and work out a pattern for you, but hopefully someone will answer before I do. Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555240 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Quote I'm just curious: if you are going to manually cut and paste a big chunk of text...why not just manually cut and paste the numbers instead? Or is that example link just a testing ground for pulling a file from somewhere else? I suck at regex too, otherwise I would have given you an answer by now. I mean, I understand the concept, but there are just like, a ton of things to memorize and I'm lazy. I don't really have nothin' better to do, so I'll try and work out a pattern for you, but hopefully someone will answer before I do. Because there is about 1000 lines of text :S Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555242 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Also, from what I can find. The regex to use to select the text between "id_ and " is.... ^"id_[-+]?\d+"$ I'm just not sure how to implement it into what I want to do. Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555249 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Quote Also, from what I can find. The regex to use to select the text between "id_ and " is.... ^"id_[-+]?\d+"$ I'm just not sure how to implement it into what I want to do. Scratch that. '/id_ (.+?) "/'; Should do fine also..... right? Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555267 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 okay, this sure as hell isn't the most elegant solution, but here goes: <?php // dummy var to hold the info because I'm not entering it from a form.. $blah=<<<F <tr> <td><input name="id_1523023" type="checkbox" /><a href="index.php?view=1523023"> <span id="label[1523023]"><img src="graphic/dots/blue.png" title="" alt="" /> <span id="labelText[1523023]">test1</span>[/url] <td><input name="id_1522728" type="checkbox" /><a href="index.php?view=1522728"> <span id="label[1522728]"><img src="graphic/dots/blue.png" title="" alt="" /> <span id="labelText[1522728]">test2</span>[/url] F; // regex to grab id_xxxxxxx ... can't figure out how to regex it without the id_, so... preg_match_all("/id_[1-9]?\d+/",$blah,$info); // reverse the array $info = array_reverse($info[0]); // so... trim the id_ off $x = 0; while ($info[$x]) { $info[$x] = ltrim($info[$x],'id_'); $x++; } // example to display it foreach($info as $i) { echo $i . "<br />"; } Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555269 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Crayon Violent, Thats bloody brilliant! Thank you very, very much! I'm sure i'll be back tomorrow with more silly question. Until then... Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555276 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 ha. not that brilliant, but it works. Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555278 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Quote ha. not that brilliant, but it works. I understand it all except why you used $X here.. $x = 0; while ($info[$x]) { $info[$x] = ltrim($info[$x],'id_'); $x++; } Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555281 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 $info is an array. You could do it individually by doing $info[0] = ltrim($info[0],'id_'); $info[1] = ltrim($info[1],'id_'); or you can loop through the array positions using a variable. Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555285 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 Ok thanks. Makes sense. How are you feeling for one more question? ( high hopes I know ;-) 0 Let assume this returns X lines. I have another file with X lines also. [url=http://www.something.com/index.php?id=]Click[/url] I want to take line 1 of $info and put that number after 'id=' in line 1 of this other file. etc This is why I needed the array flipped. It is given back to front to the corresponding line in the next file. Any ideas? Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555290 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 is that the entirety of file2? Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555293 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 no. Each line looks like this: 14,14……11……[url=http://blah.com/index.php?id=XXXXXXX]Click1[/url]…...[url=http://blah.com/index.php?id=YYYYYYY]click2[/url]…… In this case, I want to replace XXXXXXX. In another case (with a different array) I will replace YYYYYYY maybe Str_replace will work here? Find XXXXXXX and replace with array ? Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555302 Share on other sites More sharing options...
JChilds Posted June 2, 2008 Author Share Posted June 2, 2008 This should work, right? str_replace(XXXXXXX,$array[0],$info) Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555303 Share on other sites More sharing options...
effigy Posted June 2, 2008 Share Posted June 2, 2008 Quote // regex to grab id_xxxxxxx ... can't figure out how to regex it without the id_, so... /(?<=id_)\d+/ Link to comment https://forums.phpfreaks.com/topic/108296-solved-how-should-i-approach-this/#findComment-555565 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.