Jump to content

Recommended Posts

 
$unitbold = preg_replace('/^[uu][Nn][ii][Tt]/', "<b>Unit Information</b>", $objComport->ReadString());

 

What if i wanted to replace multiple words in a string? For instance, this takes the word UNIT ( or any other capitalization of it ) and makes it bold. What if I wanted to find UNIT, make that bold, Find QTY, make that bold, and find AMOUNT, and make that bold? Can you do it all in one preg_replace?

 

Thanks in advance! :)

Link to comment
https://forums.phpfreaks.com/topic/143550-preg-replace-question/
Share on other sites

Ahhh right, well if you are going to use this is a live environment I'd used str_ireplace()

 

But for learning why not try...

 

<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

//straight from php.net (hope it makes sense)
?>


<?php
$search = array("UNIT", "QTY", "AMOUNT");
$replace = array("<tr><td><b>UNIT</b></td>", "<td><b>QTY</b></td>", "<td><b>AMOUNT</b></td></tr>"); 
$string = "<table>UNIT QTY AMOUNT</table>";
$tablestring = str_replace($search, $replace, $string); 
echo $tablestring;
?>

 

A lot faster.

soo.. heres what I have so far..

 

 
$search = array("UNIT", "QTY", "AMOUNT", "1c", "10c");
$replace = array("<tr><td><b>UNIT</b></td>", "<td><b>QTY</b></td>", "<td><b>AMOUNT</b></td></tr>", "Pennies", "Dimes");
$string = $objComport->ReadString();
$tablestring = str_replace($search, $replace, $string); 
echo $tablestring;

 

Heres the output of $tablestring:

UNIT QTY AMOUNT
Pennies 18 0.18+
Dimes 3 0.30+
1 1 1.00+
TOTAL 1.48+

 

I need to get the 1 (under the word "Dimes") to say Dollars.

 

So would I need to throw out the str_replace and go with preg_replace? Not sure how to do this.

Thanks for the help, you guys rock :)

Ahhh right, well if you are going to use this is a live environment I'd used str_ireplace()

 

But for learning why not try...

 

<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

//straight from php.net (hope it makes sense)
?>

 

I'm going to play arround with this.. Think this is what I'm looking for.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.