Jump to content

Recommended Posts

Hiya peeps!

 

If I was to have something like this.

 

{POST = test}
{GET = testtwo}
<html>
<title>This is a test</title>
<body>
Test Body.
</body>
</html>

 

How do I get {POST = test} & {GET = testtwo} (they could be anything not just POST and GET) and store them in an array EG.

array('POST' => 'test', 'GET' => 'testtwo');

 

then remove them from the html file and echo it.

 

Many thanks,

 

James.

 

Link to comment
https://forums.phpfreaks.com/topic/201812-how-do-i-do-a-match-set-and-remove/
Share on other sites

Hiya Ken,

 

Thanks for the reply, basically Im trying to do this.

 

Result before filtered.

{POST = test}
{GET = testtwo}
<html>
<title>This is a test</title>
<body>
Test Body.
</body>
</html>

 

Result after filtered.

<html>
<title>This is a test</title>
<body>
Test Body.
</body>
</html>

array('POST' => 'test', 'GET' => 'testtwo');

 

So basically I'm trying to search from {POST = test} & {GET = testtwo} in the html file that is stored in a var EG.

$html = '{POST = test}
{GET = testtwo}
<html>
<title>This is a test</title>
<body>
Test Body.
</body>
</html>';

 

And turn the values that are in brackets into and array. The values may vary so it might not be POST or GET.

 

Many thanks,

 

James.

 

 

You are going to need to use preg_match_all() to do this; at least, that is probably the cleanest solution.  I am not very good with regular expressions, but I think it would be something like this:

$html = '{POST = test}
{GET = testtwo}
<html>
<title>This is a test</title>
<body>
Test Body.
</body>
</html>';
$regEx = '/\{(.*?) ?= ?(.*?)\}/';
$found = array();
$data = array();
if (preg_match_all($regEx, $html, $found, PREG_SET_ORDER)) {
  // Found at least one
  foreach ($found as $entry) {
    $data[$entry[1]] = $entry[2];
  }
}
// now remove the stuff
$html = preg_replace($regEx, '', $html);

 

Someone in the PHP RegEx Section may be able to give you a better (correct?) regular expression; as I said, I'm not very good with it.

 

Disclaimer: This code is untested, and may be completely wrong.

Maybe you could echo the $html to a file and then:

$fh = fopen("myfile.txt", "r");

while(i=0,i<2,i++)
{
$line = fgets($fh);
        array=('1stpart of line one' => 'second part of line one', '1stpart of line two' => 'second part of line two');
echo $array[0];
echo $array[1];
}

You are going to need to use preg_match_all() to do this; at least, that is probably the cleanest solution.  I am not very good with regular expressions, but I think it would be something like this:

$html = '{POST = test}
{GET = testtwo}
<html>
<title>This is a test</title>
<body>
Test Body.
</body>
</html>';
$regEx = '/\{(.*?) ?= ?(.*?)\}/';
$found = array();
$data = array();
if (preg_match_all($regEx, $html, $found, PREG_SET_ORDER)) {
  // Found at least one
  foreach ($found as $entry) {
    $data[$entry[1]] = $entry[2];
  }
}
// now remove the stuff
$html = preg_replace($regEx, '', $html);

 

Someone in the PHP RegEx Section may be able to give you a better (correct?) regular expression; as I said, I'm not very good with it.

 

Disclaimer: This code is untested, and may be completely wrong.

 

That worked amazingly! Thank you very very much!

 

Many thanks,

 

James.

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.