Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-22-2008, 12:56 AM pretty help please!
Registered User

Posts: 1
Name: lando
Trades: 0
Hi guys, i have 2 questions that i hope someone will help me on. Because its killing me, i can't solve it.


1) Write a function that will convert an array of lower case strings passed by reference into an array of upper case strings

e.g function convert(&$array_strings){
//insert code
}

I tried this:
function convert(&$array_strings)
{
$array_strings="hello this is php coding in lower case to upper case";
$higher=strtoupper($array_strings);
print("$higher");
}


2)Using regular expressions generate a function that will check if a filename stored in a string is of the correct format. The function should return a Boolean result. Any 1 upper case letter, followed by 3 lowercase letters, followed by any digit, followed by a .php or .PHP file
eg. Gone8.php

I tried this:
function regular_expressions($boolean)
{
if(preg_match("/[A-Z]{1}[a-z]{3}[0-9][.]p[Hh]p/","Gone8.php"))
{
return 1;
if ($boolean == 1)
{
print("true<br/>");
}
return false;
}


Extra: what happens if you try "Gone8.phpProblem" Does it return true or false ? Can you fix the problem if there is one?" --->Can anyone explain the answer and rationale behind it to me ?

Many thanks!

Last edited by smirnoff85; 11-22-2008 at 01:01 AM..
smirnoff85 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-22-2008, 02:05 AM Re: pretty help please!
Junior Talker

Posts: 2
Trades: 0
If you want to call your array by reference and convert it to uppercase then you can do this

PHP Code:

//Set array
$arr = Array("bill""bob""joan");

//then call the function
convert($arr);

//output will be
//BILL
//BOB
//JOAN


//define function
function convert(&$array_data){
  foreach(
$array_data as $data){
    
$higher strtoupper($data);
    echo(
$higher);
  }


that should work for what your trying to do. It will convert an array of strings by reference to uppercase. PHP does not do C style references if that is what you are looking for.
blackthought286 is offline
Reply With Quote
View Public Profile
 
Old 11-22-2008, 06:36 AM Re: pretty help please!
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
How about using array_walk to apply function to each element of array. Is passed by reference not by value.
PHP Code:
<?php
$fruits 
= array("lemon","orange","banana","apple");

function 
upper_case(&$item1)
{
    
$item1 strtoupper("$item1");
}



array_walk($fruits'upper_case');

print_r($fruits);// Array (     [0] => LEMON     [1] => ORANGE     [2] => BANANA     [3] => APPLE )
?>

Edited to add Blackthoughts does not pass by reference. $arr remains lowercase

print_r($arr); // Array ( [0] => bill [1] => bob [2] => joan )

Last edited by maxxximus; 11-22-2008 at 06:53 AM..
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 11-22-2008, 10:40 AM Re: pretty help please!
Junior Talker

Posts: 2
Trades: 0
the way I did it does pass by reference but when you use strtoupper it creates a new string instead of changing the original. If you want to change the original then maxxximus way is what you want to go with.

edited: If you want to change by reference and change the original array without calling an extra function then you can use this way.

PHP Code:
//Set array
$arr = Array("bob""bill""jane");

//then call the function
convert($arr);


print_r($arr);
//output will be
//Array ( [0] => BOB [1] => BILL [2] => JANE ) 


//define function
function convert(&$array_data){
  foreach(
$array_data as &$data){
    
$data strtoupper($data);
  }


Last edited by blackthought286; 11-22-2008 at 11:11 AM..
blackthought286 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to pretty help please!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13911 seconds with 12 queries