Gingechilla Posted February 25, 2011 Share Posted February 25, 2011 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? Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted February 25, 2011 Share Posted February 25, 2011 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 Quote Link to comment Share on other sites More sharing options...
Gingechilla Posted February 25, 2011 Author Share Posted February 25, 2011 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]); Quote Link to comment Share on other sites More sharing options...
Gingechilla Posted February 25, 2011 Author Share Posted February 25, 2011 Oh! I'm such an idiot! I did work! Thank you so much! However... It only works on the first 'Tom'. If I type in Tom twice ect only the first one changes. Quote Link to comment Share on other sites More sharing options...
Gingechilla Posted February 25, 2011 Author Share Posted February 25, 2011 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 ) Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted February 25, 2011 Share Posted February 25, 2011 Yeah no sorry about that, typo, its counting the array, not the subarray, thats why its counting two (as print_r shows there are 2 sub-arrays in the $matches array). Quote Link to comment Share on other sites More sharing options...
Gingechilla Posted February 25, 2011 Author Share Posted February 25, 2011 OK feeling a bit silly now... but where exactly is the typo I need to change to get it to work? Quote Link to comment Share on other sites More sharing options...
ChemicalBliss Posted February 25, 2011 Share Posted February 25, 2011 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.