super substr()?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
\$a="123456"
\$b="45678"

If want \$c become "12345678", that mean I want combine two strings
with overlapping each other. How to?
thanks
from Peter (cmk128@hotmail.com)

Re: super substr()?

peter wrote:

What happen if the two strings are say 123456456 456879?

you haven't completely specified the problem. Doing that will lead you
directly to an algorithm for the answer.

Re: super substr()?

The Natural Philosopher wrote:

This will take only that part of \$a until it finds a character equal to
the first character of \$b and then append \$b.

Untested (check on the  -1)
\$c = substr(\$a, 0, strpos(\$b, \$substr(\$b, 0, 1)-1)) . \$b;

Re: super substr()?

On 1 Aug, 16:34, sheldonlg <sheldonlg> wrote:

But that won't work if \$a=3D"1423456"

Re: super substr()?

Captain Paralytic wrote:

I repeat what I just said:  "This will take only that part of \$a until
it finds a character equal to the first character of \$b and then append \$b."

Re: super substr()?

On 1 Aug, 17:52, sheldonlg <sheldonlg> wrote:

Ahh, I thought you had posted it as a possible solution to the OP's
question. Since this wasn't the case, why did you post it?

Re: super substr()?

Captain Paralytic wrote:

It did solve his _particular_ example. :-)  I posted it because it
showed him some methods to use.  He would have to build upon that for a
more general case, but since he did not define what that more general
case was, I had no way to suggest anything beyond what I did.

Re: super substr()?

peter wrote:

Peter,

Maybe something like this?

<?php

function superstrcat(\$str1, \$str2) {
\$len1 = strlen(\$str1);
\$len2 = strlen(\$str2);

\$len = min(\$len1, \$len2);  // Max chars to compare

for (\$i = \$len - 1; \$i >= 0; \$i--)
if (substr(\$str1, \$len1 - \$i, \$i) == substr(\$str2, 0, \$i))
break;

return \$str1 . substr(\$str2, \$i);
}

echo superstrcat("123456", "45678") . "\n";
echo superstrcat("12345", "3456789") . "\n";
echo superstrcat("123456", "98765") . "\n";

?>

Output:

12345678
123456789
12345698765

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: super substr()?

On 8=E6=9C=882=E6=97=A5,B8=8A=E5=8D=883=E6=99=8214=E5=88=86, Jerry Stu=