Hi, I have got an array with many words and links. Iwant to have array with links starting ONLY from http://example.co,/cat/a/ What should I do ? Thanks
using array_filter it would like this $old = array(........); function start_with_your_string ($var) { $string = 'http://example.co.uk/something/'; return (strpos($var, $string) === 0); } $new = array_filter($old, "start_with_your_string"); just change $string to whatever you need.
Or just do this: $old = array(.........); $string = 'http://example.co.uk/something/'; $new = array(); foreach( $old as $k => $v ) { if( strpos( $v, $string ) !== false ) $new[] = $v; } PHP:
This code has a small bug, it just checks if the string *contains* the specified substring and not if it begins with it. You might simply add a condition like this: if( strpos( $v, $string ) !== false && strpos( $v, $string ) < 1 ) $new[] = $v;
foreach($array as $item) { if (preg_match("/example.com\/ca\/a\//i",$item)) { array_push($final_array,$item) ; } } print_r($final_array) ;
You'd want to foreach through it and use preg_match("/$http:\/\/example\.com/i, $value), replacing example\.com with whatever the full check you want to do is.