How do I escape angle brackets?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mxsmanic

    How do I escape angle brackets?

    I have a line like this in some PHP code on my Web page:

    $callthis =~ s/<[^>]*>//g;

    When the page runs, I get this error:

    Parse error: parse error, unexpected '<' in /Feedback.php on line 145

    So I presume I must escape angle brackets somehow in the regular
    expression. How do I do it? I tried putting a backslash in front of
    each angle bracket, but that doesn't seem to work (same error).

    The purpose of the statement is to remove HTML tags from the string.

    --
    Transpose hotmail and mxsmanic in my e-mail address to reach me directly.
  • Ken Robinson

    #2
    Re: How do I escape angle brackets?


    Mxsmanic wrote:[color=blue]
    > I have a line like this in some PHP code on my Web page:
    >
    > $callthis =~ s/<[^>]*>//g;
    >
    > When the page runs, I get this error:
    >
    > Parse error: parse error, unexpected '<' in /Feedback.php on line 145[/color]

    That doesn't look like any PHP code that I know.
    Use the function strip_tags() to strip HTML from strings.

    Ken

    Comment

    • Chris Hope

      #3
      Re: How do I escape angle brackets?

      Mxsmanic wrote:
      [color=blue]
      > I have a line like this in some PHP code on my Web page:
      >
      > $callthis =~ s/<[^>]*>//g;[/color]

      Looks like Perl to me.
      [color=blue]
      > When the page runs, I get this error:
      >
      > Parse error: parse error, unexpected '<' in /Feedback.php on line 145
      >
      > So I presume I must escape angle brackets somehow in the regular
      > expression. How do I do it?[/color]

      With a \ but I doubt that's the problem.
      [color=blue]
      > I tried putting a backslash in front of
      > each angle bracket, but that doesn't seem to work (same error).[/color]

      That's because it's not PHP.

      Try this: $callthis = preg_replace("/<[^>]*>/", "", $callthis);
      [color=blue]
      > The purpose of the statement is to remove HTML tags from the string.[/color]

      Try using the strip_tags() function: http://www.php.net/strip_tags

      --
      Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

      Comment

      • Mxsmanic

        #4
        Re: How do I escape angle brackets?

        Ken Robinson writes:
        [color=blue]
        > That doesn't look like any PHP code that I know.[/color]

        Doesn't PHP support substitution with regular expressions?

        I'm always getting confused between Perl and PHP.
        [color=blue]
        > Use the function strip_tags() to strip HTML from strings.[/color]

        Thanks, I'll do that instead.

        --
        Transpose hotmail and mxsmanic in my e-mail address to reach me directly.

        Comment

        • Mxsmanic

          #5
          Re: How do I escape angle brackets?

          Chris Hope writes:
          [color=blue]
          > Looks like Perl to me.[/color]

          It would work in Perl. Doesn't PHP support regular expressions? I can
          never remember which language supports what.
          [color=blue]
          > Try using the strip_tags() function: http://www.php.net/strip_tags[/color]

          Done, and it works. Thanks.

          --
          Transpose hotmail and mxsmanic in my e-mail address to reach me directly.

          Comment

          • Chris Hope

            #6
            Re: How do I escape angle brackets?

            Mxsmanic wrote:
            [color=blue]
            > Chris Hope writes:
            >[color=green]
            >> Looks like Perl to me.[/color]
            >
            > It would work in Perl. Doesn't PHP support regular expressions? I
            > can never remember which language supports what.[/color]

            Yes, PHP does support regular expressions. I gave you the code for it.
            Here it is again:

            $callthis = preg_replace("/<[^>]*>/", "", $callthis);

            --
            Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

            Comment

            • n

              #7
              Re: How do I escape angle brackets?

              Mxsmanic wrote:[color=blue]
              > Ken Robinson writes:
              >
              >[color=green]
              >>That doesn't look like any PHP code that I know.[/color]
              >
              >
              > Doesn't PHP support substitution with regular expressions?
              >
              > I'm always getting confused between Perl and PHP.
              >
              >[color=green]
              >>Use the function strip_tags() to strip HTML from strings.[/color]
              >
              >
              > Thanks, I'll do that instead.
              >[/color]

              I just wanted to add that the code looks like perl.

              T

              Comment

              • greyfade@gmail.com

                #8
                Re: How do I escape angle brackets?


                Mxsmanic wrote:[color=blue]
                > I have a line like this in some PHP code on my Web page:
                >
                > $callthis =~ s/<[^>]*>//g;
                >
                > When the page runs, I get this error:
                >
                > Parse error: parse error, unexpected '<' in /Feedback.php on line 145
                >[/color]
                this is because you've tried to use a PERL expression. in PHP, you'd
                do something more like this:

                $callthis = preg_replace("\ <[^\>]*\>","",$callth is);

                but as another poster mentioned, PHP already has a function for the
                purpose you specified.

                hope that clears things up for you.

                Comment

                Working...