/    Sign up×
Community /Pin to ProfileBookmark

Keywords and "

I want to create a script that will turns words into arrays. Not a problem. All I need is to use something like this:

[B]$array_keys = explode($kw,’ ‘);[/B]

And the words separated by spaces would each become a keyword.

But how do I make sure a phrase like this one [B]“I need this”[/B] can become a keyword of its own just because it is between ” ” ‘s ?

My idea is turning “I need this” into “I%23%need%23this23%”, so explode won’t separate the words. After explode has been used, I can use [B]str_replace[/B] to turn “I%23%need%23this23%” back into I need this.

But if I have this,

people “I need this” society “Mary Smith”

How do I make PHP recognize the strings inside ” ” ‘s ? and put %23% inside them?


____________________________________________________________

[url]http://www.carbotek.org[/url]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 09.2007 — Here's a slightly different approach:
[code=php]
$string = "word1 word2 "phrase one" word3 'phrase two'";
preg_match_all('/(?<=')[^']*(?=')|(?<=")[^"]*(?=")|bS+b/', $string, $matches);
printf("<pre>%s</pre>", print_r($matches, 1));
[/code]

output:
<i>
</i>word1 word2 "phrase one" word3 'phrase two'

Array
(
[0] =&gt; word1
[1] =&gt; word2
[2] =&gt; phrase one
[3] =&gt; word3
[4] =&gt; phrase two
)

Copy linkTweet thisAlerts:
@rpcarnellauthorJun 09.2007 — It works, thanks.

But now I'll get a headache trying to understand what all that jargon means. ?
Copy linkTweet thisAlerts:
@rpcarnellauthorJun 10.2007 — Thanks for your help.

I think there's a problem with words the may come after a quote. My messy script doesn't run into that problem, but it is a lot longer.

And yes it is resolved.

<?php

//---- Initialisation

$str="I need that "this is quote 1" people are "this is quote2" crows in the nest " this is quote 3" beautiful";

echo "<br>The string is "".$str.""</br/><br />";

$counter = strlen($str);

$o = 0;

for($i=0; $i<=$counter; $i++){

if($str[$i]=='"') //We use the length of the string to figure out where the "'s are located.

{

$output[$o] = $i; //Each position-value of " becomes a value in the output array.

$o++;

}

}

$output2 = count($output); //now we determine how many occurrences of " there are.

$L = 0;

$LL = 1;

$ui = 1;

foreach ($output as $tryit)

{

$exp[$L] = $output[$L];

if ($LL == 2 && $ui == 1)

{

$eye[$L] = substr($str, $output[$L - 1] +1, ($output[$L] - $output[$L - 1] -1)); //We take every phrase inside "" out of the string.

$LL = 1;

$ui = 0;

}

if ($ui == 0 && $LL ==2) //and using $ui, we make sure words between quotes aren't included ---> "Quote 1" extra words "Quote 2"

{

$ui = 1;

$LL = $LL - 1;

}

$L++;

$LL++;

}

$T = 0;

$itt = explode('"', $str); //All words are separated by "'s, even the ones that aren't quotes.

$str2 = "";

foreach ($itt as $tryit)

{

$kw[$T] = $tryit;
if ($kw[$T] == $eye[$T])
{
$kw[$T]= preg_replace("/(s)/","@quote@",$kw[$T]); //The quotes will be @quote@ between their spaces.
}
$str2 .=$kw[$T]; //$str2 becomes the new version of the original $str string.
$T++;


}

$str = $str2; //$str has now been altered.

$str2 = explode(" ", $str); //Now we can get keywords and quotes.

$counting = 1;

foreach ($str2 as $keywords)

{

$keywords =preg_replace("/@quote@/"," ",$keywords);
echo "<br>($counting) ".$keywords."</br>";
$counting++;

}

?>
×

Success!

Help @rpcarnell spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...