note 73312 added to language.types.string
| From: | thomas dot oldburydotnospam at tgohome dot com | Date: | Sat, 17 Feb 2007 11:33:57 +0000 |
| Subject: | note 73312 added to language.types.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123507@lists.php.net to get a copy of this message | ||
This function I created to insert a string at a position or an array of positions. Quite useful.
<?php
/*
string strwpos (string insert, string input, int position [, int mode])
Returns a string with a string inserted at a position,
or an array of positions. Normally, the string is inserted
on the right of the position, but it can be inserted on the
left or both. The following constants define where the string
should be inserted:
INS_LEFT - insert the string left of the position
INS_RIGHT - insert the string right of the position
INS_BOTH - insert the string on both sides
Returns resulting string on success, FALSE on failure.
*/
define('INS_LEFT', 0);
define('INS_RIGHT', 1);
define('INS_BOTH', 2);
function strwpos($ins, $string, $pos, $mode = INS_RIGHT)
{
$str = "";
for($c = 0; $c < strlen($string); $c++)
{
if(is_array($pos))
{
if(in_array($c, $pos, true))
{
if($mode == INS_RIGHT)
{
$ins_c = substr($string, $c, 1) . $ins;
}
elseif($mode == INS_LEFT)
{
$ins_c = $ins . substr($string, $c, 1);
}
elseif($mode == INS_BOTH)
{
$ins_c = $ins . substr($string, $c, 1) . $ins;
}
else
{
trigger_error("Unknown mode '$mode' specified", E_USER_WARNING);
return false;
}
$str .= $ins_c;
}
else
{
$str .= $string[$c];
}
}
else
{
if($c == $pos)
{
if($mode == INS_RIGHT)
{
$ins_c = substr($string, $c, 1) . $ins;
}
elseif($mode == INS_LEFT)
{
$ins_c = $ins . substr($string, $c, 1);
}
elseif($mode == INS_BOTH)
{
$ins_c = $ins . substr($string, $c, 1) . $ins;
}
else
{
trigger_error("Unknown mode '$mode' specified", E_USER_WARNING);
return false;
}
$str .= $ins_c;
}
else
{
$str .= $string[$c];
}
}
}
return $str;
}
?>
This function can have a few problems. For example, the following code will produce an unexpected
result:
<?php echo strwpos('love, not ', 'I hate you', 2); ?>
It will produce this result:
I hlove, not ate you
The reason for this is because the string is inserted to the right of the string by default. So
using the optional MODE parameter with the INS_LEFT value will produce the result we need.
<?php echo strwpos('love, not ', 'I hate you', 2, INS_LEFT); ?>
This will produce the result we need:
I love, not hate you
You can also use an array of places to insert the string into:
<?php echo strwpos('brown ', 'The quick fox jumped over the lazy dog',
array(10, 35), INS_LEFT); ?>
This will produce the result:
The quick brown fox jumped over the lazy brown dog
Hope this helps.
----
Server IP: 194.145.210.3
Probable Submitter: 81.86.162.169
----
Manual Page -- http://www.php.net/manual/en/language.types.string.php
Edit -- https://master.php.net/note/edit/73312
Del: integrated -- https://master.php.net/note/delete/73312/integrated
Del: useless -- https://master.php.net/note/delete/73312/useless
Del: bad code -- https://master.php.net/note/delete/73312/bad+code
Del: spam -- https://master.php.net/note/delete/73312/spam
Del: non-english -- https://master.php.net/note/delete/73312/non-english
Del: in docs -- https://master.php.net/note/delete/73312/in+docs
Del: other reasons-- https://master.php.net/note/delete/73312
Reject -- https://master.php.net/note/reject/73312
Search -- https://master.php.net/manage/user-notes.php