TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-05-2009, 01:37 PM   #1 (permalink)
The Contributor
 
hello-world's Avatar
 
Join Date: Feb 2009
Posts: 73
Thanks: 30
hello-world is on a distinguished road
Default Basic Loop with [ str_replace]

I have a problem with replacing and looping.I need your help please to change
This line
Quote:
The quick brown {fox} jumps over the lazy {dog}
To this
Quote:
The quick brown cat jumps over the lazy mouse.
PHP Code:
$string 'The quick brown {fox} jumps over the lazy {dog}';
$arr = array("fox" =>"Cat","dog"=>"mouse");
preg_match_all("/{.*?}/",$testing,$matches); 
By the way how to practice looping in php ?
Is there any good exercise for it ?
hello-world is offline  
Reply With Quote
Old 09-05-2009, 05:08 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You don't necessarily need a loop to achieve this. You could do the following:

php Code:
$szItem   = 'The quick brown {fox} jumps over the lazy {dog}';
$aReplaceThese  = array('fox', 'dog');
$aReplaceWidth  = array('cat', 'mouse');

$szItem         = str_replace($aReplaceThese, $aReplaceWidth, $szItem);
echo $szItem;

Although, if you wish to practice loops, then I could show you how to achieve it with a foreach loop.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-05-2009, 05:25 PM   #3 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

or you can also use the translate function:
php Code:
<?php
$string = 'The quick brown {fox} jumps over the lazy {dog}';
$arr = array("{fox}" => "cat","{dog}" => "mouse");
echo 'before: ' . $string . "\n";
$string = strtr($string, $arr);
echo 'after: ' . $string;
?>
tony is offline  
Reply With Quote
Old 09-06-2009, 06:31 PM   #4 (permalink)
The Contributor
 
hello-world's Avatar
 
Join Date: Feb 2009
Posts: 73
Thanks: 30
hello-world is on a distinguished road
Default

Thanks alot WildHoney and Tony.
hello-world is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Nested while loop needed for this? gtifllm General 1 08-17-2009 07:50 PM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
Horizontal nested loop problem Dracula General 3 06-05-2008 03:12 PM
Another quetion - Loop inside a loop Jmz General 1 05-30-2008 09:07 PM


All times are GMT. The time now is 06:31 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design