Jump to content

[SOLVED] How should I approach this?


JChilds

Recommended Posts

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

 

<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
Share on other sites

$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
Share on other sites

$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.