Jump to content

Regex Replacing Within an area

- - - - -

  • Please log in to reply
4 replies to this topic

#1
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
I am trying to replace tabs that exist within paragraph tags. Here are a few attempts I made. I can't think of a good way to do this without a script. I was hoping some of you regex gurus could help me out! :D

<?PHP
$var = "    <p>        this is         data    </p><p>Hello    </p>    ";
/*
    Output:(?:<p>)[\t]+
        "    <p>this is data</p><p>Hello</p>    "
*/

// Attempt 1
$pattern = "/[\t]+/";
// preg_match_all($pattern, $var, $matches);
// print_r($matches);

// Attempt 2
$pattern = "/(?:<p>)[\t]+/";
// preg_match_all($pattern, $var, $matches);
// var_dump($matches);

// Attempt 3
$pattern = "/(?:<p>)(?:[^\t]+)?[\t]+/";
// preg_match_all($pattern, $var, $matches);
// var_dump($matches);

// Attempt 4
$pattern = "/(<p>)(([^\t<]+)?([\t]+)([^\t<]+)?)+(<\/p>)/";
preg_match_all($pattern, $var, $matches);
var_dump($matches);
?>


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Quick question: what do you want the tabs replaced with? Also, any reason you aren't using preg_replace? PHP: preg_replace - Manual
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
With nothing. I want them removed.

I would use preg_replace if I could select them properly. I can't seem to get it right. I simply was using the preg_match_all to see what I was selecting for confirmation I was doing it correctly.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Quick question: what editor did you make that with? I know that when I use jEdit, it replaces tabs with 2 spaces. When highlighting the text above, it didn't look like there were any tab characters in it, just spaces.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Notepad++ FTW!

The tabs appear correctly in my IDE but not when I paste it online it appears. I even tried putting it on pastebin.

I just tried to cover all my cases. A paragraph starting with a tab and a character. One paragraph starts with a tab one does not, one ends with a tab and the other one does not. Inside the paragraphs I just put them in their randomly.

Thanks for looking into this for me. [:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users