adrek Posted June 17, 2009 Share Posted June 17, 2009 i have this function that takes a a series of numbers serpearted by a comma and inserts a period into that place in a string. function addChar($string, $csv){ $array = explode(",", $csv); $init = strlen($string); if($init < $array[sizeof($array)-1]) $init = $array[sizeof($array)-1]; $k=0; for($i=0; $i<=$init; $i++){ if($i == $array[$k]){ $string2 .= "."; $k++; } $string2 .= $string[$i]; } return $string2; } like if i input addChar("Dr No", "2") that works fine but if i do addChar("A.B.C.", "1,3,5") i get "A.BC..". anyone know what might be wrong? Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/ Share on other sites More sharing options...
adrek Posted June 17, 2009 Author Share Posted June 17, 2009 like if i input addChar("Dr No", "2") that works fine but if i do addChar("A.B.C.", "1,3,5") i get "A.BC..". anyone know what might be wrong? sorry this was a type. if i put in ("Dr No", "2") i get Dr. No but if i put in addChar("ABC", "1,3,5") i get "A.BC.." Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-858256 Share on other sites More sharing options...
MadTechie Posted June 18, 2009 Share Posted June 18, 2009 but if i put in addChar("ABC", "1,3,5") i get "A.BC.." What was you expecting ? i assume the number mean after X chars from the left So A. <-- . = After #1 A.B. <-- . = After #3 A.B.C <-- after 4 no change A.B.C. <-- after 5 add one which is the same as adding after 4 Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-858536 Share on other sites More sharing options...
adrek Posted June 18, 2009 Author Share Posted June 18, 2009 the numbers are the spot of the string where the period would go. and since strings are an array of characters the index starts at 0. so basically the 2nd, 4th, 6th characters should be periods which would be represented like so respectively string[1], string[3], string[5] Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-858650 Share on other sites More sharing options...
Ken2k7 Posted June 18, 2009 Share Posted June 18, 2009 The function should be more accurately named to addDot instead of addChar. It really doesn't make sense to label it 1,3,5. Try 1,2,3 with this function - <?php function addDot ($str, $csv) { $csv = explode(',',$csv); $str = str_split($str); foreach ($csv as $key) { $str[$key] = isset($str[$key])? '.' . $str[$key] : '.'; } return join($str); } $e = addDot('ABC', '1,2,3'); var_dump($e); Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-858672 Share on other sites More sharing options...
adrek Posted June 18, 2009 Author Share Posted June 18, 2009 i have a site with a series of articles and what im trying to do is strip out the punctuation so that the urls are easier to read. A.B.C. is the orginal name of the article so i convert that to abc for the url. i pass it through a function to get the place of the characters <?php function charPlace($string, $char){ for($i=0; $i<strlen($string); $i++){ if($string[$i] == $char) $value .= $i.","; } $value = substr($value, 0, strlen($value)-1); return $value; } ?> so executing charPlace("A.B.C.", "."); would return 1,3,5 and i store those values in a cookie. after doing that i strip the punctation. so the final url will be http://mysite.com/articles/2009/abc/ when the page is accessed the cookie with the charPlace values will be read and the addDot function would be called to restore the punctuation and turning it back into A.B.C. to get the article from the database. the problem that im running into is the string is not restored to what it was, thus not returning any results. Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-858717 Share on other sites More sharing options...
Ken2k7 Posted June 18, 2009 Share Posted June 18, 2009 Uh, did you just change the problem? Anyway, if you want to remove the dots in 'A.B.C' so it looks like 'ABC', then you can do - $str = 'A.B.C.'; $str = str_replace('.','',$str); Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-859271 Share on other sites More sharing options...
adrek Posted June 19, 2009 Author Share Posted June 19, 2009 sorry if im not clear. no i didnt change the problem. ive got the script removing the dots. but in the database the title is with the dots so in order me for me to get the result that im looking for i need to put the dots back. here is the psuedocode for what should be happening loop: get article title get the placement of the dots in the title and store them in a cookie generate links based off the titles and make them search engine friendly. ex: "Dr. Sues" becomes "dr-sues" when link is clicked add the dots to "dr-sues" based off values from the cookie restore spaces get results from database using the string "dr. sues". if it helps ill just post all relevant code <?php // adds the dots to the title that were previouly taken out function addDot($string, $csv){ $array = explode(",", $csv); $init = strlen($string); if($init < $array[sizeof($array)-1]) $init = $array[sizeof($array)-1]; $k=0; print $init; for($i=0; $i<=$init; $i++){ if($i == $array[$k]){ $string2 .= "."; $k++; } $string2 .= $string[$i]; } return $string2; } // gets the pace of the specified character function charPlace($string, $char){ for($i=0; $i<strlen($string); $i++){ if($string[$i] == $char) $value .= $i.","; } $value = substr($value, 0, strlen($value)-1); return $value; } // creates cleaner URIs based of the article title function createLink($title){ $link = strtolower(str_replace(" ", "-", $title)); $link = str_replace(".", "", $link); return $link; } // get the articles from the database $sql = "SELECT * FROM articles"; // if an article title is set then find that specified article if (!empty($_GET['title'])) { $csv = addslashes($_COOKIE['csv']); $title = addslashes($_GET['title']); $title = str_replace("-", " ", $title); $title = addDot($title, $csv); $sql .= " WHERE title='$title"; $article = true; } $query = mysql_query($sql); if(!$article){ // print out links while($array = mysql_fetch_array($query)){ $title = $array['title']; $csv = charPlace($title, "."); $uri = createLink($title); $link .= "<a href=\"http://mysite.com/$uri/\" onClick=\"createCookie('csv', '$csv');\">$title</a>"; } echo $link; } elseif ($article){ //Display Article } ?> createCookie() is a javascript function im using to create the cookie with the csv varibles Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-859393 Share on other sites More sharing options...
Ken2k7 Posted June 19, 2009 Share Posted June 19, 2009 Wait, so what's wrong with the function I wrote? Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-859396 Share on other sites More sharing options...
adrek Posted June 19, 2009 Author Share Posted June 19, 2009 wow im stupid. your function works great. thanks alot. i just didnt rewrite the charPlace function to work right with it. thanks alot i appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/162617-solved-string-manipulation/#findComment-859686 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.