Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying Multiple Records Functions

Status
Not open for further replies.

jgd12345

Technical User
Apr 22, 2003
124
GB
Hi, I need todo 3 functions todo the following to some text:
1. remove all occurances of <br />
2. changing first letter of each line to a capital
3. and first letter of each word to a capital

I'd be very greatful if you could help. Thanx
 
Hi jgd12345,

Sounds to me like you need a regular expression...

part1 can easily be done with a str_replace function e.g.
$string = str_replace(&quot;<br />&quot;, &quot;&quot;,$string)

parts2 and 3 can be done at the same time with a relatively simple regular expression. unfortunately my regex is very rusty!

cheers

Phil Clare
 
ok, time to shed some rust!

try:

$string = preg_replace(&quot;\b(.)/e&quot;,strtoupper('\\1')&quot;,$string);

let me know what happens
 
Cheers, I'll let you know how it goes as soon as I've tested it (at uni at mo and can't test). It's quite important that the function for 2 and 3 are different because I need to use them in different situations. I think the one you posted above is the solution to the third one. Thanx
 
I assume you are aware that student posting is not allowed in this forum?
 
That's abit mean, it's not for a uni project because we don't get the pleasure of using php for a project which I'd probably breeze. It's for a website
I need the functions to sort out the tabs in the database. Hope that clears things up.
 
sorry, I wasn't trying to be mean I was just checking!

to replace the first letter of the line with upper case just change the word boundary characters in the regular expression to the newline character... i.e.

$string = preg_replace(&quot;\n(.)/e&quot;,strtoupper('\\1')&quot;,$string);
 
Hey, I checked your functions by running the script at the bottom and the outputted text was blank shown below. Note I corrected a parse error in your solution:

<?

$string = &quot;Am I correct to defend the fist that holds this pen?<br />
It's ink that lies,<br />
the pen, the page, the paper.<br />
I live, I learn.<br />
You will always take what i have earned.<br />
And so aid my end while I believe I'm winning.&quot;;

$string2 = &quot;Good to know that if I had attention&quot;;

$string = str_replace(&quot;<br />&quot;, &quot;&quot;, $string);
$string2 = preg_replace(&quot;\n(.)/e&quot;, strtoupper('\\1'), $string2);
$string = preg_replace(&quot;\b(.)/e&quot;, strtoupper('\\1'), $string);

echo &quot;String 1: $string<br>&quot;; // Returned value of String 1:
echo &quot;String 2: $string2<br>&quot;; // Returned value of String 2:

?>

I'd be greatful if you could show me what is the problem. Thanx
 
You might need to wrap your regexes in delimiters i.e. change them to:
$string2 = preg_replace(&quot;/\n(.)/e&quot;, strtoupper('\\1'),$string2);
$string = preg_replace(&quot;/\b(.)/e&quot;, strtoupper('\\1'),$string);

Hope this helps,

Phil Clare
 
Nope still didn't work. The errors I got are below:

$string2 = preg_replace(&quot;/\n(.)/e&quot;, strtoupper('\\1'),$string2); // This just didn't change the string
$string = preg_replace(&quot;/\b(.)/e&quot;, strtoupper('\\1'),$string); // Returned an error
 
Hi, another thing I really need help with is how to cut a string to a certain length. I found the function mb_strcut from php.net/manual but I need to install another module to get this working. If someone could tell me an alternative I'd be greatful. Thanx
 
If by cut a string to a certain length, you mean just strip it past X number of characters, that's easily done with the substr() function.

$string = substr($string, 0, 50); //Return the first 50 characters of $string.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top