Need help writing ereg...

  • quantumcloud
  • Proficient
  • Proficient
  • User avatar
  • Posts: 456
  • Loc: Dhaka, Bangladesh

Post 3+ Months Ago

Well, I have been trying and I don't know what I am missing. Here is what I want to do:

Replace this: [anything="regular expression goes here"]
With: empty string or " " .

I want to use the ereg function for that. I tried many variations to match the pattern without success. One example:

PHP Code: [ Select ]
$text = ereg_replace("^[\[]+[.]+[\]]+$", "", $text);


For someone experienced it'll probably take a minute to get the right pattern. Where am I doing wrong?[/code]
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

try:
Code: [ Select ]
/^\[[^\]*]$/


This pattern matches your string, although without more context, I don't know if this will actually work for you...

*EDIT: I threw in the / delimiters... they are not technically part of the pattern of course.

.c
  • quantumcloud
  • Proficient
  • Proficient
  • User avatar
  • Posts: 456
  • Loc: Dhaka, Bangladesh

Post 3+ Months Ago

Well, it is not working. There is not much more details to share here. Retrieve a string like this from the database:

[abcd="efghijks"]bnbnsb nsb snbsn

And strip it off of anything inside the brackets, including the brackets. When you test you can just use this example to get rid of [...] and output the rest of the string.

Could you please check out with your pattern, if it works for it? I did, and it is not working for me. :(
  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1260
  • Loc: ./

Post 3+ Months Ago

$text =~ s/\[.*\=\".*\"\]//;

I think
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

Ok, on second look, my previous attempt wasn't quite right.

This should do it, I think. Achored at the start, fine an open bracket. Then between the open bracket, find one or more characters that isn't a close bracket until you get to a close bracket, then save anything that comes after it and replace the whole string with that. This is Perl, I'm blanking on the PHP translation off the top of my head.

Code: [ Select ]
$cmd = s/^\[[^\]+]\](.*)/$1/ig;


Give that a try and see. Sorry I couldn't be more help with PHP... I keep all my PHP resources at my home office where I do freelance work, since my work is an ASP shop. Helps keep me honest and ethical =]

.c
  • quantumcloud
  • Proficient
  • Proficient
  • User avatar
  • Posts: 456
  • Loc: Dhaka, Bangladesh

Post 3+ Months Ago

Thank you guys. We finally did it!

Though none of the ones suggested worked exactly. I kept tweaking both of yours and one variation of this213's worked!

The working pattern is like this:
PHP Code: [ Select ]
$text = ereg_replace("\[.*\=\".*\"\]", "replacement", $text);


:)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 130 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.