FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to use ereg_replace to remove something?





devroom
Hi

A string: "/** remove remove */ here here here".

How can i use ereg_replace to let /** until */ be removed, so that only "here here here", will be left?

ereg_replace("/** remove remove */", '', $str) is not an option.
It has to be something like this: "'`\[b\](.+?)\[/b\]`is',"
(ripped from a bbcode converter)

Please tell me also what the logic is.

Grtz and thanks in advance Cool
hexkid
devroom wrote:
How can i use ereg_replace to let /** until */ be removed, so that only "here here here", will be left?

I never used ereg_replace(), so I don't know how to do it with that function. If you're willing to try preg_replace() instead (but I don't think so as you specifically asked for ereg_replace()) I know how to do it Smile
devroom
hexkid wrote:
devroom wrote:
How can i use ereg_replace to let /** until */ be removed, so that only "here here here", will be left?

I never used ereg_replace(), so I don't know how to do it with that function. If you're willing to try preg_replace() instead (but I don't think so as you specifically asked for ereg_replace()) I know how to do it Smile


Because I want to totally remove /** until */, preg_replace says: "Noting to copy". And thats correct, because i just want to erase it.
hexkid
devroom wrote:
Because I want to totally remove /** until */, preg_replace says: "Noting to copy".
I've never seen "Nothing to copy" in relation to preg_replace(). Can you provide a sample code that says that? Thanks.

Code:
$rx = '/\/\*.*\*\//sU';

$data = '/** remove remove */ here here here';
echo preg_replace($rx, '', $data);

$data = 'a/*b*/c/*d*/e';
echo preg_replace($rx, '', $data); // the "U" in the $rx is used here

$data = 'line 1
/* line 2 */
line 3'; // the "s" in the $rx is used here
echo nl2br(preg_replace($rx, '', $data));
devroom
hexkid wrote:
devroom wrote:
Because I want to totally remove /** until */, preg_replace says: "Noting to copy".
I've never seen "Nothing to copy" in relation to preg_replace(). Can you provide a sample code that says that? Thanks.

Code:
$rx = '/\/\*.*\*\//sU';

$data = '/** remove remove */ here here here';
echo preg_replace($rx, '', $data);

$data = 'a/*b*/c/*d*/e';
echo preg_replace($rx, '', $data); // the "U" in the $rx is used here

$data = 'line 1
/* line 2 */
line 3'; // the "s" in the $rx is used here
echo nl2br(preg_replace($rx, '', $data));


Thank you very much.
It is working now. I had to make the function preg_replace.

"Nothing to copy" --> My fault. I dont know the logic of it. Can you explain that to me?
hexkid
hexkid wrote:
Code:
$rx = '/\/\*.*\*\//sU';

$data = '/** remove remove */ here here here';
echo preg_replace($rx, '', $data);

$data = 'a/*b*/c/*d*/e';
echo preg_replace($rx, '', $data); // the "U" in the $rx is used here

$data = 'line 1
/* line 2 */
line 3'; // the "s" in the $rx is used here
echo nl2br(preg_replace($rx, '', $data));
Actually the comment about the "s" is wrong; sorry. The "s" is used when the stuff between /* and */ spans several lines
Code:
$data = 'line 1
/* line 2
line 3 */
line 4'; // the "s" in the $rx is used here


devroom wrote:
Thank you very much.
It is working now. I had to make the function preg_replace.

"Nothing to copy" --> My fault. I dont know the logic of it. Can you explain that to me?
Not without seeing your code.
devroom
hexkid wrote:
devroom wrote:
"Nothing to copy" --> My fault. I dont know the logic of it. Can you explain that to me?
Not without seeing your code.


I better had putted a new line in in.
I meant: what is the logic of using the \/\/, the . and the sU, etc
hexkid
devroom wrote:
what is the logic of using the \/\/, the . and the sU, etc


/\/\*.*\*\//sU
These are the delimiters for the regular expression. They are not really a part of it, but you have to have them in (they could be other characters)

/\/\*.*\*\//sU
One slash; as slashes are special characters in regular expressions, when you want to match a literal slash you have to "escape it".

/\/\*.*\*\//sU
Similarly for the asterisk.

/\/\*.*\*\//sU
This matches any character (including new lines because of the "s") any number of times (0 or more).

The "U" is to make the match ungreedy. Without it, in the string "a/*b*/c/*d*/e" the match would be "a/*b*/c/*d*/e" instead of two different matches ("a/*b*/c/*d*/e" and "a/*b*/c/*d*/e").

Anyway, it's all explained on the manual: http://php.net/preg_replace
Related topics
What Programs should I unintall from this list?
how remove forced ads?
How to remove a bootscreen?
Account activation, email....
resolved Win32.Beoven can't remove it =(
Fix or remove the featured advertisements box
How to remove advertisements in .tk
i want to remove the borders around images with links.
Not able to Remove application in Control Panel?
Add/remove WinXp SP2 components issue...
Remove Ads from Yahoo Messenger!
How to remove file sharing from Windows for good?
dot.tk how to remove ads it works for me
Remove "Users browsing this forum" in PHPBB
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.