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 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    String Manipulation problem

    Hi,

    I have a big chunk of HTML stored in a variable $html

    All the links are in caps, is there any way I could convert them to just have the first letters of the words capitalised without having to do it manually. Bear in mind I'm just looking to do this for the text contained within the <a> tags on not the text outside these?

    Thanks in advance

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    yo

    there is php function to upper case only the first letter of each word, it is upwords..

    PHP Code:
    $bar 'HELLO WORLD!';
    $bar ucwords($bar);             // HELLO WORLD!
    $bar ucwords(strtolower($bar)); // Hello World! 
    To do it only for your <a href> links you would need to create a foreach, whack the $html value into an array.. then you can single out the a href and run ucwords on it.

    Hope that helps,
    cheers,
    Sir P

  • #3
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    CSS

    a {
    text-transform: capitalize;
    }
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #4
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sir pannels View Post
    yo

    there is php function to upper case only the first letter of each word, it is upwords..

    PHP Code:
    $bar 'HELLO WORLD!';
    $bar ucwords($bar);             // HELLO WORLD!
    $bar ucwords(strtolower($bar)); // Hello World! 
    To do it only for your <a href> links you would need to create a foreach, whack the $html value into an array.. then you can single out the a href and run ucwords on it.

    Hope that helps,
    cheers,
    Sir P
    I'm a bit of a PHP newbie so could you please tell me how I would og about doing this? Thanks again

  • #5
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    It's a red herring denham. ucwords() and strtolower() are applied to the entire html code block in your scenario. It won't work. You need to either use the above css solution (the easiest choice) or check out how to apply text formatting using regular expressions while the html output is held in the buffer. Similarly to how search engine hilighting works.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #6
    New Coder
    Join Date
    Sep 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Something like...

    PHP Code:
    <?php

    $html 
    'The sky was blue <a href=\'/path/index.php\'>bLuE sKy</a> and pretty too!';

    echo 
    preg_replace "/(?=[^>]+<\/a>)([a-z0-9 ]+)/eis""ucwords ( strtolower ( '$1' ) )"$html);

    ?>


  •  

    Posting Permissions

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