using & in an html code

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
kiwichick
Posts: 735
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

using & in an html code

Post by kiwichick »

Hi there I have the following html "replacer" script that I use many times a day:

Code: Select all

"Replacer|:rename"
   end(getinfo("CountSelected") < 1), "Nothing selected!";
   $html = urldecode(html('
      <html>
      <body style="font-family:arial;">
      <form method="get" action="xys:">
      Replace This: <INPUT TYPE="TEXT" NAME="Search" SIZE="60" VALUE=""><BR>
      With This: &nbsp;&nbsp; &nbsp; <INPUT TYPE="TEXT" NAME="Replace" SIZE="60" VALUE="">  <BR>
      <p align="left"><INPUT  type="submit" name="Rename" value="  Replace  " ></p>   
      </form>
      </body>
      </html>',"480", "260", "Renamer"));

   $Search = gettoken($html, "1", "&");
   $Replace = gettoken($html, "2", "&");

   $Search = gettoken($Search, "2", "=");
   $Replace = gettoken($Replace, "2", "=");

   rename s, "$Search/$Replace";
   
It works great except for one problem - when & is entered in either field:

replace & with -
nothing changes
ball&chain remains ball&chain

replace - with &
- is replaced with nothing
ball-chain becomes ballchain

I didn't really write the code so I don't know how to fix it but am I right in thinking it has something to do with these two lines of code:

Code: Select all

   $Search = gettoken($html, "1", "&");
   $Replace = gettoken($html, "2", "&");
Thanks in advance for any help :-)
Windows 10 Pro 22H2
Scaling 150%

highend
Posts: 15016
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: using & in an html code

Post by highend »

Code: Select all

"Replacer|:rename"
   end(getinfo("CountSelected") < 1), "Nothing selected!";
   $html = urldecode(html('
      <html>
      <body style="font-family:arial;">
      <form method="get" action="xys:">
      Replace This: <INPUT TYPE="TEXT" NAME="Search" SIZE="60" VALUE=""><BR>
      With This: &nbsp;&nbsp; &nbsp; <INPUT TYPE="TEXT" NAME="Replace" SIZE="60" VALUE="">  <BR>
      <p align="left"><INPUT  type="submit" name="Rename" value="  Replace  " ></p>   
      </form>
      </body>
      </html>',"480", "260", "Renamer"));

   $Result = replace(regexreplace($html, "\?Search=|&Rename=\s+Replace\s+", ""), "&Replace=", ":");

   $Search = gettoken($Result, "1", ":");
   $Replace = gettoken($Result, "2", ":");

   rename s, "$Search/$Replace";
One of my scripts helped you out? Please donate via Paypal

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: using & in an html code

Post by Stefan »

kiwichick wrote:
It works great except for one problem - when & is entered in either field:
That is because the "&" sign is used by the HTML protocol
to delimiter the different returned parts:
?Search=THIS&Replace=THAT&Rename= Replace
?Search=THIS & Replace=THAT & Rename= Replace


If your script now return an "&" you end up with two "&"
and so gettoken() takes the wrong one:
?Search=&&Replace=&&Rename= Replace
?Search=& & Replace=& & Rename= Replace

Add a "step;" command and watch the debugger to spot the issue.


One way would be, like highend posted, a other method to get the result from the html() command return.



Another way would be, if you like the step-by-step method and dislike regex,
to mask the "&" signs with an uncommon temporary-replacement-sign.

Code: Select all

"Replacer|:rename"
   end(getinfo("CountSelected") < 1), "Nothing selected!";
   $html = urldecode(html('
      <html>
      <body style="font-family:arial;">
      <form method="get" action="xys:">
      Replace This: <INPUT TYPE="TEXT" NAME="Search" SIZE="60" VALUE=""><BR>
      With This: &nbsp;&nbsp; &nbsp; <INPUT TYPE="TEXT" NAME="Replace" SIZE="60" VALUE="">  <BR>
      <p align="left"><INPUT  type="submit" name="Rename" value="  Replace  " ></p>   
      </form>
      </body>
      </html>',"480", "260", "Renamer")); 

//    step; // open the debugger


   //mask the &-sign and the following button-id with an temporary string:
     $html = replace($html,"&Replace", ">DELIM<Replace");
     $html = replace($html,"&Rename", ">DELIM<Rename");
   //replace every remained "&" with an temporary replacement string:
     $html = replace($html,"&", ">ATSIGN<");
   //rename the "&" plus button-id back to origin string:
     $html = replace($html,">DELIM<Replace", "&Replace");
     $html = replace($html,">DELIM<Rename", "&Rename");

   //Now gettoken() have a change to correctly split the returned string at the "&" signs as always:
   $Search = gettoken($html, "1", "&");
   $Replace = gettoken($html, "2", "&");

   $Search = gettoken($Search, "2", "=");
   $Replace = gettoken($Replace, "2", "=");


  //at the end, replace the temporary replacement of the "&" back to the origin "&" sign:
   $Search = replace($Search,">ATSIGN<", "&");
   $Replace = replace($Replace,">ATSIGN<", "&");


  // rename s, "$Search/$Replace";

   text "$Search/$Replace";

Result debugging:

User input:
Replace: This&This
With: That&That


replace()
----
?Search=This&This&Replace=That&That&Rename= Replace
----
&Replace
----
>DELIM<Replace




set
----
$html
----
?Search=This&This>DELIM<Replace=That&That&Rename= Replace



Results in:
This&This/That&That

kiwichick
Posts: 735
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: using & in an html code

Post by kiwichick »

Thanks for the help guys! highend's solution works perfectly - much appreciated!!
Windows 10 Pro 22H2
Scaling 150%

Post Reply