0

I have a little issue with deleting a part of string using php. Let me get you a little bit into problem, I have vlariable "column1text" in which is text string endered into < textarea >, but that does not matter. I want to remove all HTML tags from this string since they are doing problems sometimes. For example: When this string contains < /body > or < /table > it will do a lot of unwanted mess. So I want to get these tags away.

I am using $column1text = str_replace("TEXT TO REMOVE", "", $column1text); and it works, but I want to make function for it (optionaly if you know easier way, just tell me, I'll be glad).

I am using this function: function remove($removetext) { $column1text = str_replace($removetext, "", $column1text); }

And I am using it like this: remove("TEXT TO REMOVE");

What am I doing wrong? (I am sure it's something pretty silly, but I cannot find it!)

P.S. I am totally sorry about my English, it must sound stupid, but I had no other idea than asking you.

3 Answers 3

4

You can either pass in $column1text as reference, or have your function returns the modified text (which I prefer)

 function remove($column1text, $removetext) {
   return str_replace($removetext, "", $column1text);
 }

$column1text = remove($column1text, '<span>');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I am so glad it works :-) Only... Is that '<span'> or '<span>'? Because > is away from rest... Is that right or missclick?
0

You are passing by value, when you actually need to be passing by reference. If you don't know what the difference is I suggest reading up a little on it.

Link

edit:

In a nutshell, whenever you pass a function a parameter, that function makes a copy of whatever it was passed, so when that parameter is manipulated in the function, it manipulates the copy and not the passed object itself.

When a function has a parameter passed by reference, then it actually has access to the object that was passed itself. That means anything you do to manipulate that object also affects the object outside of the function (since its not just a copy of it you are working with).

2 Comments

Is this your attempt at answering the question?
@FreshPrinceOfSO No, I hit submit too early by accident. I edited to elaborate.
0

You need to tell interpreter that you want to use global variable, because now it sees it as local variable (within your function). To do this you just need to use global keyword:

function remove($removetext) { 
    global $column1text;
    $column1text = str_replace($removetext, "", $column1text); 
}

you can read more about it here http://php.net/manual/en/language.variables.scope.php

1 Comment

Though it works, I would recommend staying away from global variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.