Jump to content

preg_match_all Array from preg_match_all


Gingechilla

Recommended Posts

Hello again,

 

I have some form data, which I then search through for particular code data like so:

$html2 = $_POST['fname'];
preg_match_all("/<bla>(.*)<\/bla>/", $html2, $matches40);

 

So the above searches for all the data between <bla>XXXXXX</bla> from $POST

 

Which I then print to my page using:

(Only so I can see while developing)

print_r($matches40);

 

This displays HTML output like so:

Array ( [0] => Array ( [0] => Hello [1] => My [2] => Name [3] => Is [4] => Tom ) [1] => Array ( [0] => Hello [1] => My [2] => Name [3] => Is [4] => Tom ) )

 

What I am trying to do is again use the preg_match_all function to look through the array output and find data that I want to remove. E.g. If one of the variables from $matches40 is 'Tom' I want to find and replaces this with 'Ben'.

 

I spent a day searching Google but to not success. Any help?

 

Link to comment
Share on other sites

Depending on your intended data you will have to sift through this could be quite an resource intensive method, but something like:

 

<?php

$matches = ... (from preg_match);
$matches_count = count($matches);

$replace_array = array(
"Tom",
"John",
"Edward"
);

$replacewith_array = array(
"Ben",
"John2",
"EdWaRd....."
);

for($i=0;$i<$matches_count;$i++){
$matches[1][$i] = str_replace($replace_array,$replacewith_array,$matches[1][$i]);
}

print_r($matches[1]);

?>

 

hope this helps

Link to comment
Share on other sites

Thanks for the fast reply,

 

It just seems to print out the following:

 

Array ( [0] => Ben [1] => Cliff [2] => Dave [3] => Ned [4] => Austin )

 

HTML Input:

<bla>Tom</bla>
<bla>Cliff</bla>
<bla>Dave</bla>
<bla>Ned</bla>
<bla>Austin</bla>

 

PHP Code:

 

$html2 = $_POST['fname'];

preg_match_all("/<bla>(.*)<\/bla>/", $html2, $matches40);
print_r($matches40);


$matches = $matches40;
$matches_count = count($matches);

$replace_array = array(
		 "Tom",
		 "John",
		 "Edward"
					   );

$replacewith_array = array(
		"Ben",
	        "John2",
		"EdWaRd....."
						   );

for($i=0;$i<$matches_count;$i++){
	$matches[1][$i] = str_replace($replace_array,$replacewith_array,$matches[1][$i]);
}

print_r($matches[1]);

 

Link to comment
Share on other sites

Actually maybe there is something wrong with the first array:

 

preg_match_all("/<bla>(.*)<\/bla>/", $html2, $matches40);
print_r($matches40);

 

When I echo the count:

$matches = $matches40;
$matches_count = count($matches);
echo count($matches);

It only counts 2 and changes two of the 'Tom' to 'Ben'

 

--------------

HTML Input:

<bla>Tom</bla>
<bla>Tom</bla>
<bla>Tom</bla>
<bla>John</bla>
<bla>Cliff</bla>
<bla>Tom</bla>

--------------

Array Out:

Array ( [0] => Ben [1] => Ben [2] => Tom [3] => John [4] => Cliff [5] => Tom )

 

Link to comment
Share on other sites

the count, as you said its only counting 2, because its counting the "parent" array, which has 2 "sub-arrays". But those sub-arrays each hold all the matches, so you need to count one of the sub-arrays (all the sub-arrays of preg_match will have the same number of items, but some specific items in some sub-arrays may be blank, depending on your regular expression).

 

hope this helps ;)

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.