mIRC Home    About    Download    Register    News    Help

Print Thread
#105146 13/12/04 02:42 AM
Joined: Dec 2004
Posts: 5
C
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
C
Joined: Dec 2004
Posts: 5
Hi,

Sorry if I'm asking a dumb question, I just dont know too much about regexp but I seemed to compile a little bit.

I want to change all backslashes \ to forward slashes /, that I have in many html files, however I only want to do it for slashes within the <a> tag. I have 2 started solutions that I cant seem to get over a hump with.

1.
<a[^>]*>
This will highlight all my link tags. I would like to use <a[\\]*> instead but then there is no padding. THe only way that will work is for one slash and if the html link looks like <a\>, and that will never occur.
I need this to work on html tags like the following example:
<a href = "blah\blah\blah..." >
and avoid all other slashes outside this tag.

2.
Solution to was provided by a friend who doesnt have much knowledge, but more than me and he wanted to use a lookback but again ran into trouble with a fixed length variable.
(?<=<a)\\(?=>)
I dont understand this method too much.

Thanks for you time.

#105147 13/12/04 11:37 AM
Joined: Dec 2002
Posts: 1,922
O
Hoopy frood
Offline
Hoopy frood
O
Joined: Dec 2002
Posts: 1,922
This one appears to work.

alias FixSlash1 {
  • var %a = $1
    while $regsub(%a, /(\G(?(?<=^).*?<a)[^>]*?)\\/gi, \1/, %a) { }
    return %a
}


//echo -a $FixSlash1(<a href = "blah\blah\blah...">)
gives
<a href = "blah/blah/blah...">

#105148 13/12/04 01:57 PM
Joined: Dec 2004
Posts: 5
C
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
C
Joined: Dec 2004
Posts: 5
Thanks for your response, what language was that script written for?
Will that work in PHP?
THanks

#105149 13/12/04 03:06 PM
Joined: Nov 2003
Posts: 2,327
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Nov 2003
Posts: 2,327
It was written for mIRC, my php is rusty but I think this should work.

Code:
if (!function_exists(FixSlash1)) {
  function FixSlash1($text) {
    $text = preg_replace("\\1\/",$text,"/(\G(?(?&lt;=^).*?&lt;a)[^&gt;]*?)\\/gi")
    return $text
  }
}


New username: hixxy
#105150 13/12/04 03:18 PM
Joined: Dec 2002
Posts: 1,922
O
Hoopy frood
Offline
Hoopy frood
O
Joined: Dec 2002
Posts: 1,922
Whatever works with mIRC's $regsub should work with PHP's preg_* functions, but there's a better way to do that in PHP. Try this code:

Code:
&lt;?
 
  function FixSlash($str)
  {
    $re  = '/&lt;a [^&gt;]+&gt;/ei';
    $sub = 'str_replace(chr(92), chr(47), "\0")';
    return preg_replace($re, $sub, $str);
  }
 
  $text = '&lt;A HREF="a\b\c"&gt;a\b\c&lt;/A&gt;';
 
  echo FixSlash($text);
 
?&gt;

#105151 13/12/04 09:21 PM
Joined: Dec 2004
Posts: 5
C
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
C
Joined: Dec 2004
Posts: 5
You guys rock,

Im not to sure I understnad all the syntax in the regexps you guys provided but I will give them a try. Will they work for 0..n amount of backslashes?

THanks very much, you guys rock!

#105152 13/12/04 10:47 PM
Joined: Dec 2002
Posts: 1,922
O
Hoopy frood
Offline
Hoopy frood
O
Joined: Dec 2002
Posts: 1,922
Yes, there should be no limitations with the PHP version.

If you forget my first reply with the complex mIRC alias and focus only on the PHP version, you can see that it takes every occurrence of the <A> tag and passes it through str_replace(), which, in turn, ensures that all backslashes (chr 92) within the current tag are replaced with forward-slashes (chr 43).

You can find more examples that use preg_replace() in the manual.

#105153 14/12/04 09:37 PM
Joined: Dec 2004
Posts: 5
C
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
C
Joined: Dec 2004
Posts: 5
Hi Online, I tried your solution and it worked for the test string however when I passed your FixSlash function an html string, not all the link backslashes were changed. The file I tested was:
http://www.ctremblay.com/EQT_front_page.htm
I have about 2000 like this.

Also, I tried using my reg exp instead:
'<a[^>]*>'
but got a warning error, unkown modifier ']'

not sure why that didnt work. Anyways, any suggestions?

Thanks so much!!!

Oh yea, I havent tried any other solutions yet, I have a nice class setup in PHP that sorts through all files/folders and picks out a file type specified. SO i just need to do the pregexp. Any suggestions are greatly appreciated.!!!

#105154 15/12/04 02:39 PM
Joined: Dec 2004
Posts: 5
C
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
C
Joined: Dec 2004
Posts: 5
It looks as if I need to know the syntax to replace a back slash with a foward slash. I have:
preg_replace('/<a[^>]*>/', '/',$str);

THis is replaceing all the hyperlinks with a slash, I dont know much about regexp, but I need to replace it with the hyperlink just modifying \ with /.
THanks

#105155 16/12/04 11:46 AM
Joined: Dec 2002
Posts: 1,922
O
Hoopy frood
Offline
Hoopy frood
O
Joined: Dec 2002
Posts: 1,922
Sorry for the late reply. Try to change

$re = '/<a [^>]+>/ei';

to...

$re = '/<a\s+[^>]+>/ei';

The difference between the two is that the former requires that a single space follow the "<A", while the latter accepts any whitespace character that follows (space, tab, newline etc). This makes the pattern compatible with tags that occupy multiple lines like this...

Code:
...
  &lt;A                    [color:blue]&lt;-- newline[/color]
href="moo"&gt;...&lt;/A&gt;
...


Link Copied to Clipboard