Help - Search - Members - Calendar
Full Version: a perl question
Movable Type Community Forum > Additional Resources > Tips and Tricks
Mr. Adams
Hello I don't really know perl that well, but I'm trying to modify something & wanted to know if anyone could help me.
How would I go about only allowing & returning a value of true if the "img src" tag was used in a string & return a value of false if any other tags are used when evaluating a string? Kind of like what the Sanitize plugin does. thanks
oscarf
CODE
$text =~ m/img\ssrc/;


Then you can treat the whole expression as a boolean which will be true if there's a match.
Mr. Adams
oscarf the problem with that is that it also returns true if other html tags are entered in edition to the img src tag. I only want it to allow the img tag in the string.
oscarf
Unnghh ...  sad.gif

Well, a brute force look-ahead match might do it ...

Make a list of tags you don't want, and for purposes of the following expression, you can string them together in an OR fashion by separating them with |

So, try something like this (where BADTAG's you don't want, and GOODTAGS, you do) - it should return TRUE if pattern BADTAG does not match, and pattern GOODTAG does:

CODE
$text =~ /(?=^(?:(?!BADTAG1|BADTAG2).)*$)GOODTAG/s;


Let me know if you can get it to work  tongue.gif

Reference is:

Perl Cookbook
oscarf
The absence of profuse and gushing thanks for my regex tip here suggests to me:
1) You have not been back to the forum
2) You believe that line noise contributed to my suggestion
3) You can't get it to work any better than I can

In the case of #3, if I were you, I might try just writing a few likely tags into a nested "if" statement ...
CODE
if ($text=~ m/img\ssrc/) {
  if ($text !~ /<br>|<em>|<a|<p>|<h/)
  {print $text}
  }

I just put in a few tags, and I have no idea whether something like that would meet your needs. Anyway, that one just prints the line if it is TRUE that "img src" IS in the text, AND the other tags are not,  but you could do whatever you wanted, obviously.

Otherwise, true HTML tags parsing is quite the task. I'd suggest you take a look at Brad's code in the Sanitize module (or the plugin) to see what his parsing subroutine actually does.

Why turn to Brad? Cuz he's much smartrn me!  tongue.gif
Mr. Adams
sorry, I havent been back. I've been working long hours & havent had time. I do appreciate your help being that no one else suggested anything to me. I'm trying it out now biggrin.gif
oscarf
Re-reading my post it almost looked like I was serious. I was just trying to squeeze in my (stolen) line-noise joke (you gotta admit there's a fine line between a long regex and a bad modem connection), as well as an acknowledgment that I couldn't even get the thing to work reliably. Anyway, I hope one of those works for you, and feel free to post or email if not.
Mr. Adams
thanx oscarf I finally got the chance to sit down & try it out... It worked like a charm, ty  :D
oscarf
My pleasure.  Thanks for the followup  :)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.