php strpos example
Example 1: php check if string contains word
$myString = 'Hello Bob how are you?';
if (strpos($myString, 'Bob') !== false) {
echo "My string contains Bob";
}
Example 2: strpos in php
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?>
Example 3: strpos php
<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
if ($pos !== false) {
echo "La cadena '$findme' fue encontrada en la cadena '$mystring'";
echo " y existe en la posición $pos";
} else {
echo "La cadena '$findme' no fue encontrada en la cadena '$mystring'";
}
?>
Example 4: strpos
$mystr = 'abc';
$findMe = 'a';
$position = strpos($mystr, $findMe);
Example 5: strpos in php
<?php
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1);
?>
Example 6: strpos php
<?php
function g($string,$start,$end){
preg_match_all('/' . preg_quote($start, '/') . '(.*?)'. preg_quote($end, '/').'/i', $string, $m);
$out = array();
foreach($m[1] as $key => $value){
$type = explode('::',$value);
if(sizeof($type)>1){
if(!is_array($out[$type[0]]))
$out[$type[0]] = array();
$out[$type[0]][] = $type[1];
} else {
$out[] = $value;
}
}
return $out;
}
print_r(g('Sample text, [/text to extract/] Rest of sample text [/WEB::http://google.com/] bla bla bla. ','[/','/]'));
?>
results:
Array
(
[0] => text to extract
[WEB] => Array
(
[0] => http://google.com
)
)
Can be helpfull to custom parsing :)