note 104072 added to function.str-replace
| From: | biohazard dot ge at gmail dot com | Date: | Fri, 20 May 2011 21:33:36 +0000 |
| Subject: | note 104072 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179918@lists.php.net to get a copy of this message | ||
Replace and match strings between two needles in large haystack quicker (?):
<?php
//Assume you have input:
$input = "<p> Images
<img src='some/image_1.jpg'> This is a cat
and <img src=some/image_2.jpg> That is a bat
and <img src="some/image_3.jpg"> What is this
and <img src=some/image_4.jpg> What is that
</p>
";
between_replace ('<img src="','">', $input, "{*}");
between_replace ('<img src=\'','\'>', $input, "{*}");
between_replace ('<img src=','>', $input, "'{*}'");
//As result we have
//$input == "<p> Images
// <img src='some/image_1.jpg'> This is a cat
//and <img src='some/image_2.jpg'> That is a bat
//and <img src='some/image_3.jpg'> What is this
//and <img src='some/image_4.jpg'> What is that
//</p>
//";
$result = between_fetch ("<img src='","'>", $input);
// $result == array ('some/image_1.jpg', 'some/image_2.jpg',
'some/image_3.jpg', 'some/image_4.jpg');
?>
Not a good example but only with them and str_replace I parsed word converted html for epub. This is
what people invent not willing to get familiar with regular expressions...
<?php
function between_replace ($open, $close, &$in, $with, $limit=false, $from=0)
{
if ($limit!==false && $limit==0)
{
return $in;
}
$open_position = strpos ($in, $open, $from);
if ($open_position===false)
{
return false;
};
$close_position = strpos ($in, $close, $open_position+strlen($open));
if ($close_position===false)
{
return false;
};
$current = false;
if (strpos($with,'{*}')!==false)
{
$current = substr ($in, $open_position+strlen($open),
$close_position-$open_position-strlen($open));
$current = str_replace ('{*}',$current,$with);
//debug_echo ($current);
}
else
{
$current = $with;
}
$in = substr_replace ($in, $current, $open_position+strlen($open),
$close_position-$open_position-strlen($open));
$next_position = $open_position + strlen($current) + 1;
if ($next_position>=strlen($in))
{
return false;
}
if ($limit!==false)
{
$limit--;
}
between_replace ($open, $close, $in, $with, $limit, $next_position);
return $in;
}
function between_fetch ($open, $close, &$in, &$result=null, $with='',
$limit=false, $from=0)
{
if ($limit!==false && $limit==0)
{
return $in;
};
if (!is_array($result))
{
$result = array ();
}
$open_position = strpos ($in, $open, $from);
if ($open_position===false)
{
return false;
};
$close_position = strpos ($in, $close, $open_position+strlen($open));
if ($close_position===false)
{
return false;
};
$current = substr ($in, $open_position+strlen($open),
$close_position-$open_position-strlen($open));
$next_position = $open_position + strlen($current) + 1;
if ($with!='')
{
$current = str_replace('{*}', $current, $with);
}
$result[] = $current;
if ($next_position>=strlen($in))
{
return false;
}
if ($limit!==false)
{
$limit--;
}
between_fetch ($open, $close, $in, $result, $with, $limit, $next_position);
return $in;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 62.168.182.91
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/104072
Del: integrated -- https://master.php.net/note/delete/104072/integrated
Del: useless -- https://master.php.net/note/delete/104072/useless
Del: bad code -- https://master.php.net/note/delete/104072/bad+code
Del: spam -- https://master.php.net/note/delete/104072/spam
Del: non-english -- https://master.php.net/note/delete/104072/non-english
Del: in docs -- https://master.php.net/note/delete/104072/in+docs
Del: other reasons-- https://master.php.net/note/delete/104072
Reject -- https://master.php.net/note/reject/104072
Search -- https://master.php.net/manage/user-notes.php