Need regular expression or some easier way to do HTML query string text substitution

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    Need regular expression or some easier way to do HTML query string text substitution

    I am trying to replace within the HTML string $html the following:

    <a
    href="index.php ?section=image& refAction=edit& previousRefOrig =edit&chooseAlb um=1&album=here &sort=&willDesc =&willDisplayPa nel=&action=mov e_image">Go
    back..</a>
    With

    <a
    href="index.php ?section=image& refAction=edit& previousRefOrig =edit&chooseAlb um=1&album=here &sort=&willDesc =&willDisplayPa nel=&action=edi t">Go
    back..</a>
    Where I'm replacing "action=move_im age" with "action=<?=
    $_REQUEST['refAction'] ?>"

    I'm using regular expressions to do just that, or at least I'm trying:

    [PHP]
    $html = preg_replace('/([&\?])action=[^&">]*([&"])([^>]*>Go
    back.*$)/', '$1action=' . $_REQUEST['refAction'] . '&blah=foo' . $pagQS
    .. '$2$3', $html);
    [/PHP]

    I'm using "&blah=foo" as my test to see if preg_replace() has taken
    place, so far, it hasn't.

    Several class methods spawn the query string before "Go back.." several
    different ways; however, there will always be a "action=[something]"
    within the query string for you to change, just cannot predict where;
    furthermore, there MAY also include "refAction=[something]" within the
    same query string.

    How do I do this either with regular expressions or some other way?

    Thanx
    Phil

  • Oli Filth

    #2
    Re: Need regular expression or some easier way to do HTML query stringtext substitution

    comp.lang.php said the following on 26/09/2006 22:03:
    I am trying to replace within the HTML string $html the following:
    >
    <a
    href="index.php ?section=image& refAction=edit& previousRefOrig =edit&chooseAlb um=1&album=here &sort=&willDesc =&willDisplayPa nel=&action=mov e_image">Go
    back..</a>
    >
    With
    >
    <a
    href="index.php ?section=image& refAction=edit& previousRefOrig =edit&chooseAlb um=1&album=here &sort=&willDesc =&willDisplayPa nel=&action=edi t">Go
    back..</a>
    >
    Where I'm replacing "action=move_im age" with "action=<?=
    $_REQUEST['refAction'] ?>"
    >
    I'm using regular expressions to do just that, or at least I'm trying:
    <...snip...>
    Several class methods spawn the query string before "Go back.." several
    different ways; however, there will always be a "action=[something]"
    within the query string for you to change, just cannot predict where;
    furthermore, there MAY also include "refAction=[something]" within the
    same query string.
    >
    How do I do this either with regular expressions or some other way?
    IMHO, having multiple methods independently responsible for creating a
    complex URL like that, and then having a regular expression to
    subsequently "alter" it is likely to be error-prone and a maintenance
    nightmare.

    I don't know what your exact scenario is, but if I were doing it, I
    might maintain an associative array of the GET variables and values e.g.
    something like:

    class Foo
    {
    private $variables;

    function bar()
    {
    $variables["section"] = "image";
    $variables["refAction"] = "edit";
    ...
    $variables["action"] = "move_image ";
    }

    function fuzz()
    {
    $variables["section"] = "home";
    $variables["refAction"] = "delete";
    ...
    $variables["action"] = "kill_image ";
    }

    function alter_action()
    {
    $variables["action"] = "edit";
    }

    function getQueryString( )
    {
    // convert array contents to a query string
    }

    }


    --
    Oli

    Comment

    • comp.lang.php

      #3
      Re: Need regular expression or some easier way to do HTML query string text substitution


      Oli Filth wrote:
      comp.lang.php said the following on 26/09/2006 22:03:
      I am trying to replace within the HTML string $html the following:

      <a
      href="index.php ?section=image& refAction=edit& previousRefOrig =edit&chooseAlb um=1&album=here &sort=&willDesc =&willDisplayPa nel=&action=mov e_image">Go
      back..</a>
      With

      <a
      href="index.php ?section=image& refAction=edit& previousRefOrig =edit&chooseAlb um=1&album=here &sort=&willDesc =&willDisplayPa nel=&action=edi t">Go
      back..</a>
      Where I'm replacing "action=move_im age" with "action=<?=
      $_REQUEST['refAction'] ?>"

      I'm using regular expressions to do just that, or at least I'm trying:
      <...snip...>
      Several class methods spawn the query string before "Go back.." several
      different ways; however, there will always be a "action=[something]"
      within the query string for you to change, just cannot predict where;
      furthermore, there MAY also include "refAction=[something]" within the
      same query string.

      How do I do this either with regular expressions or some other way?
      >
      IMHO, having multiple methods independently responsible for creating a
      complex URL like that, and then having a regular expression to
      subsequently "alter" it is likely to be error-prone and a maintenance
      nightmare.
      >
      I don't know what your exact scenario is, but if I were doing it, I
      might maintain an associative array of the GET variables and values e.g.
      something like:
      >
      class Foo
      {
      private $variables;
      >
      function bar()
      {
      $variables["section"] = "image";
      $variables["refAction"] = "edit";
      ...
      $variables["action"] = "move_image ";
      }
      >
      function fuzz()
      {
      $variables["section"] = "home";
      $variables["refAction"] = "delete";
      ...
      $variables["action"] = "kill_image ";
      }
      >
      function alter_action()
      {
      $variables["action"] = "edit";
      }
      >
      function getQueryString( )
      {
      // convert array contents to a query string
      }
      >
      }
      >
      >
      --
      Oli

      I took your suggestion and ran with it, a much simpler solution than
      anything I could have ever done :(

      Thanx
      Phil

      Comment

      Working...