...

View Full Version : String Manipulation problem



denhamd2
05-29-2007, 11:40 AM
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 :)

sir pannels
05-29-2007, 12:37 PM
yo

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



$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

meth
05-29-2007, 12:49 PM
CSS

a {
text-transform: capitalize;
}

denhamd2
05-29-2007, 03:08 PM
yo

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



$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

meth
05-29-2007, 10:15 PM
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 (http://suda.co.uk/projects/SEHL/).

printf
05-29-2007, 11:30 PM
Something like...


<?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);

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum