WebDeveloper.com

WebDeveloper.com (http://www.webdeveloper.com/forum/index.php)
-   PHP (http://www.webdeveloper.com/forum/forumdisplay.php?f=16)
-   -   Removing common text from string (http://www.webdeveloper.com/forum/showthread.php?t=22014)

gizmo 11-22-2003 02:15 PM

Removing common text from string
 
I found the following function which appears to do what I want:
PHP Code:

function remove_from($whole, $part)
$i=0;
while ((
$part[$i]==$whole[$i])&&($i<strlen($part))) { $i++ }
$whole=substr($whole,$i,1);

but am having trouble getting it to work.
As I see it, if I have a string $whole="Hello everyone" and another $part="Hell" I should get the result as "o everyone". What happens is the I get an error message on the line containg the curly braces. Also I am not sure if I am calling the function correctly.

Kr|Z 11-22-2003 03:36 PM

That function seems to remove the word from an array.

To just remove it from a string, try something like:

PHP Code:

$whole = ereg_replace($part, "", $whole);


pyro 11-22-2003 03:50 PM

If you know anything about regex, or want to learn anything about them, you'll want to use PCRE (Perl Compatible Regular Expressions) over POSIX regex...

gizmo 11-23-2003 02:48 AM

More detail
 
I have got rid of the error message. I think, but am not certain, that there was a non-printing character there arising from a copy & paste operation.
Kr|Z, your solution looks fine, but in simplifying the problem I omitted to say that the $part could be all or part deleted by the user as for example:
$part="Enter address: ";
$whole="Enter add 8,High Street";

In this case, I want to remove up to the common characters "Enter add" and the function would appear to do that if I could call it correctly.

Pyro, don't confuse me, I'm just starting out with php;)

gizmo 11-25-2003 09:40 AM

Bump
 
I am still getting the parse error unexpected '}' in the line starting with 'while' and fail to see why.

DaiWelsh 11-25-2003 09:51 AM

The body of the function should be in braces and unless there is some code you did not show also needs to return the result
(though I am not sure if PHP funcs default to returning value of last line in which case it is not 100% necessary). Also I would put the semi-colon after i++ but again strictly it may not be requried with one line between braces.

function remove_from($whole, $part)
{
$i=0;
while (($part[$i]==$whole[$i])&&($i<strlen($part))) { $i++; }
$whole=substr($whole,$i,1);
return($whole);
}

Lastly, unless I forget the substr syntax this will only give you the first non-common character, to get the rest of the string change

$whole=substr($whole,$i,1);

to

$whole=substr($whole,$i);

I have not used array notation for strings in PHP, so I am just assuming that part is valid.

HTH,

Dai

gizmo 11-25-2003 10:10 AM

Now i get "Uninitialized string offset: 4" and I think it is the function line starting with $whole.

gizmo 11-25-2003 10:37 AM

The line reported as having the error is the 'while' line. Oh well, back to the manual.:(

DaiWelsh 11-25-2003 11:10 AM

I think we are getting somewhere :)

What values were you passing into the function? The code assumes that $part will be shorter than $whole otherwise the while loop will run past the end of the $whole string which seems to be what is happening here?

In fact looking again you may want to swap the conditions in the while:-

while ((($i<strlen($part)) and ($part[$i]==$whole[$i]))

as that will also cause a problem even if $part is shorter than $whole.

HTH,

Dai

gizmo 11-25-2003 11:21 AM

I appreciate your help, Dai, but now the error has changed to "unexpected T_VARIABLE".
$part will normally be less than $whole, but there could be an instance where it is more, so the condition should be catered for.

DaiWelsh 11-25-2003 11:27 AM

Sorry too many brackets

while (($i<strlen($part)) and ($part[$i]==$whole[$i]))

for the other part you could rewrite the code without too much effort to work whichever was shorter but since the code you have described is essentially symmetrical you could just swap the parameters round if $part is longer than $whole which would probably be simpler.

gizmo 11-25-2003 12:40 PM

Ok Dai, so the function doesn't bomb out any more but when I call the function thus:
remove_from($a1, $a2);
and add echo $whole; it says unrecognised variable ($whole that is). Am I calling the function correctly and using the result correctly? I have done this sort of thing ok with Visual Basic, but I'm struggling here.

DaiWelsh 11-26-2003 04:20 AM

I will need to see the latest versio of the function and the code calling it I think.

gizmo 11-26-2003 08:51 AM

Dai, the current error is "undefined variable" in the 'echo' line. The code is

$a1="hello its me";
$a2="hell";

function remove_from($whole, $part)
{
$i=0;
while (($i<strlen($part))&&($i<strlen($whole))&&($part[$i]==$whole[$i])) { $i++; }
$whole=substr($whole,$i);
}
remove_from($a1, $a2);
echo $whole."\n"."\n";

Your comments would be appreciated. :)

DaiWelsh 11-26-2003 12:08 PM

ok, you need to learn about variable scope to understand this problem. The variables $whole and $part only exist inside the function (between the open and close braces { ... } ) when you call the function with

remove_from($a1, $a2);

the values of $a1 and $a2 are assigned to $whole and $part inside the function, all the work is doneon $whole and $part but then they are discarded at the end of the function. To get your code to work correctly you would do something like this.

$a1="hello its me";
$a2="hell";

$a3 = remove_from($a1, $a2);
echo $a3."\n"."\n";

function remove_from($whole, $part)
{
$i=0;
while (($i<strlen($part))&&($i<strlen($whole))&&($part[$i]==$whole[$i])) { $i++; }
$whole=substr($whole,$i);
return($whole);
}

Note, I have deliberately moved the function to the bottom to make it clear that the order of the lines is not important, the code inside the function runs when you call it regardless of where in the file it is located.

When the code gets to this line

$a3 = remove_from($a1, $a2);

the function code runs and then the line return($whole) passes the value of $whole back to the calling code and it is then assigned to $a3.

If that does not make any sense then I suggets you read up on the concepts of functions, argument passing and variable scope since it is too big a topic to explain here.

HTH,

Dai


All times are GMT -5. The time now is 01:24 PM.

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.