Jump to content

[SOLVED] String Manipulation


adrek

Recommended Posts

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?

Link to comment
Share on other sites

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.."

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.