Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts

    preg_replace ^ and [-100-100]

    I'm looking for a function to replace instances of a carrot and a number to superscript. Currently, I have a function that handles positive powers below:

    function polyformal($tt)
    {
    // replace all ^ & numbers(0-9) & ("+" or "-" or ")") with sup numbers with
    for ($x=80;$x>=0;$x--)
    {
    $tt=str_replace("^" . $x . "+","<sup>" . $x . "</sup>+",$tt);
    $tt=str_replace("^" . $x . "-","<sup>" . $x . "</sup>-",$tt);
    $tt=str_replace("^" . $x . ")","<sup>" . $x . "</sup>)",$tt);
    $tt=str_replace("^" . $x,"<sup>" . $x . "</sup>",$tt);
    }

    return $tt;
    }

    I want to use preg_replace if possible to be more efficient as well as to account for any power positive or negative without having to do a large loop. I tried this below:
    $tt="4(2ax^7y^6z^3-6x^3y^4z^-825)^5";
    $tt=preg_replace('#[0-9]#','<sup>' . '#[0-9]#' . '</sup>',$tt);
    This replaces all indidual numbers with the superscript #[0-9]#, but replaces each digit of 825 with a replacement.

    My goal is to make numbers raised to a power superscript. Any ideas?

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $text preg_replace'/\^(\d+)/''<sup>$1</sup>'$text ); 

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by kbluhm View Post
    PHP Code:
    $text preg_replace'/\^(\d+)/''<sup>$1</sup>'$text ); 

    kbluhm,

    That did all positive powers, which is a great start. But it left the negative powers unchanged.

    Thanks for that portion. Any ideas on the negative power portion?

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    PHP Code:
    $tt=preg_replace('/\^(-?)(\d+)/','<sup>$1$2</sup>',$tt); 
    Remember The Thank You Button Is Your Friend =]

    [ PHP / Javascript / VisualBasic / HTML / DHTML / CSS / .NET / MySQL ]

  • #5
    Regular Coder
    Join Date
    Mar 2010
    Posts
    235
    Thanks
    39
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by JordanW View Post
    PHP Code:
    $tt=preg_replace('/\^(-?)(\d+)/','<sup>$1$2</sup>',$tt); 
    That's fantastic, thanks Jordan W!

    I've been relying on str_replace as a crutch for too long and I need to read up more on regular expressions.

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    159
    Thanks
    2
    Thanked 15 Times in 15 Posts
    Literally what I did the other week. As soon as you get the hang of it... you just cannot stop using it!

    Here is a great little tool for your reference:
    http://www.addedbytes.com/cheat-shee...eet-version-1/
    Remember The Thank You Button Is Your Friend =]

    [ PHP / Javascript / VisualBasic / HTML / DHTML / CSS / .NET / MySQL ]

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by mathceleb View Post
    kbluhm,

    That did all positive powers, which is a great start. But it left the negative powers unchanged.

    Thanks for that portion. Any ideas on the negative power portion?
    Oops, over read that portion. Sorry about that.
    PHP Code:
    $text preg_replace'/\^(\-?\d+)/''<sup>$1</sup>'$text ); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •