makeshift_theory Posted January 26, 2007 Share Posted January 26, 2007 Oh I see, it needs to remove space from the beggining and end of the string. Hold on I will revise. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-169830 Share on other sites More sharing options...
makeshift_theory Posted January 26, 2007 Share Posted January 26, 2007 [code]<?phpfunction my_trim($str,$charlist){ $pattern[0] = "/^\s+/"; $pattern[1] = "/\s+\$/"; $str = preg_replace($pattern,"",$str); if(!empty($charlist) || $charlist != '') { if(is_array($charlist)) { foreach($charlist as $char) $str = ereg_replace($char,"",$str); } else $str = ereg_replace($char,"",$str); } return $str;}$trim = " LOT OF SPACES HERE ";echo my_trim($trim,'');?>[/code]I used patterns this time. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-169837 Share on other sites More sharing options...
Orio Posted January 26, 2007 Share Posted January 26, 2007 I forgot to mention all you need to remove the spaces (including white-spaces, newlines, tabs etc as said in the manual) from the [b]beginning and end[/b] of the string. The function doesn't need to include the optional charlist parameter (although if you want to add it, you are welcome :)).Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-169850 Share on other sites More sharing options...
makeshift_theory Posted January 26, 2007 Share Posted January 26, 2007 Well the function I wrote removes space from beggining and end and if it finds a character array or string it will replace that too. Is this correct? Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-169853 Share on other sites More sharing options...
Orio Posted January 28, 2007 Share Posted January 28, 2007 Yes, this version is good. What's your challenge?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-171099 Share on other sites More sharing options...
makeshift_theory Posted January 28, 2007 Share Posted January 28, 2007 Hmmmmm okay, write a function that does the same thing as "similiar_text()." Basically the function would compare two strings and return a percentage based on the similarity between them. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-171173 Share on other sites More sharing options...
fert Posted January 30, 2007 Share Posted January 30, 2007 This code returns the number of characters that are the same in both strings [code]function check_text($string1,$string2){ $count=0; $same=0; while($string1{$count}!="" && $string2{$count}!="") { if(strcmp($string{$count},$string2{$count})) { $same++; } $count++; } return $same;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-172437 Share on other sites More sharing options...
makeshift_theory Posted January 30, 2007 Share Posted January 30, 2007 cool now take the numbers and build a percentage ;D Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-172452 Share on other sites More sharing options...
The Little Guy Posted February 2, 2007 Author Share Posted February 2, 2007 fert, that code didn't even work for me, in the strings, i did this:"abc","123"It returned 3, it should return 0, none of those were the same. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-175245 Share on other sites More sharing options...
neylitalo Posted February 2, 2007 Share Posted February 2, 2007 fert: I've never seen this, can you explain it?[code]$string1{$count}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-175247 Share on other sites More sharing options...
Daniel0 Posted February 2, 2007 Share Posted February 2, 2007 [quote author=neylitalo link=topic=119299.msg519609#msg519609 date=1170400107]fert: I've never seen this, can you explain it?[code]$string1{$count}[/code][/quote]It takes the [tt]$count[/tt]th character in string.Example: [code]<?php$string = "abcdef";echo $string{0}; // echoes the 0th character in $string (a)echo $string{2}; // echoes the 2nd character in $string (c)echo $string{2+1}; // echoes the 3rd character in $string (d)echo "\n";for($i=strlen($string)-1; $i >= 0; $i--){ echo $string{$i}; // using a variable instead of an integer}?>[/code]The above example outputs: [code]acdfedcba[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-175410 Share on other sites More sharing options...
redbullmarky Posted February 2, 2007 Share Posted February 2, 2007 neal, {} is a deprecated version of [] which is the more common way of accessing strings - ie, by treating a string like an array.so $string{$char} is the same as $string[$char] Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-175432 Share on other sites More sharing options...
neylitalo Posted February 2, 2007 Share Posted February 2, 2007 ok, that would explain it - I've never had to really pull characters out of a string, so I've never had call to use that particular notation. It's definitely good to know, though. Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-175518 Share on other sites More sharing options...
makeshift_theory Posted February 2, 2007 Share Posted February 2, 2007 You can do it with arrays too $array[$strinarray]{2}, I just used it for a project I was working on for work. It's very cool =). Quote Link to comment https://forums.phpfreaks.com/topic/31283-the-php-challenge/page/4/#findComment-175537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.