3

I have a code that will produce array of strings.... now my problem is i need to substr each result of the array but i think array is not allowed to be used in substr...

please help:

CODE:

<?php
$file = 'upload/filter.txt';
$searchfor = $_POST['search'];
$btn = $_POST['button'];
$sum = 0;

if($btn == 'search') {

//prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);

// escape special characters in the query
$pattern = preg_quote($searchfor, '/');

// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";


// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
$result = implode("\n", $matches[0]);
echo $result;


 }
else{
 echo "No matches found";
 }


 }
 ?>

The $matches there is the array... i need to substr each result of the $matches

4
  • To apply a function to all items in an array use array_map (returns a new array) or array_walk (can mutate items in the original array). Commented Sep 5, 2013 at 7:51
  • first thing your question is not clear, On which array you to use substr. Have you tried? . Good solution for any array to apply function is array_map Commented Sep 5, 2013 at 7:51
  • hi @Jon thanks for your reply... can you give a sample code if you dont mind :3 Commented Sep 5, 2013 at 7:52
  • @BOSS i need to substr each result of the $matches array... Commented Sep 5, 2013 at 7:53

3 Answers 3

3

you can use array_walk:

function fcn(&$item) {
   $item = substr(..do what you want here ...);
}

array_walk($matches, "fcn");
Sign up to request clarification or add additional context in comments.

5 Comments

can i put my $matches array in the substr? I need to substr each result of the array @matches
@Bongsky, no you cannot put $matches in substr. You have to walk over the array and apply substr to each individual element. To example I gave you will update the $matches array.
it does nothing... as ive known substr has 1st parameter string... what would be my parameters?
@Bongsky in the fcn function in my example, $item holds the value of each item, so your call would be something like $item = substr($item, 5); or so.
i get this error sir... substr() expects parameter 1 to be string, array given
3

Proper use of array_walk

array_walk( $matches, substr(your area));

Array_map accepts several arrays

array_map(substr(your area),  $matches1, $origarray2);

in your case

 array_map(substr(your area),  $matches);

Read more:

array_map

array_walk

1 Comment

what does your area implies? what are the parameters?
0

To find a sub string in an array I use this function on a production site, works perfectly.

I convert the array to a collection because it's easier to manage.

public function substrInArray($substr, Array $array) {
    $substr = strtolower($substr);
    $array = collect($array); // convert array to collection

    return $body_types->map(function ($array_item) {
        return strtolower($array_item);
    })->filter(function ($array_item) use ($substr) {
        return substr_count($array_item, $substr);
    })->keys()->first();

}

This will return the key from the first match, it's just an example you can tinker. Returns null if nothing found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.