Jump to content

Get word count index within exploded array.


ryancooper

Recommended Posts

I'm using the code below to on echo certain words based on their index/key value, this works fine however, im trying to only do this in exploded quotes... the problem is, exploding quotes turns the portion into an array which cant be passed to str_word_count... how can i do the same thing only inside quotes. So that  hello world "this is a test" would become:

Hello World This [0] => Is [1] => a [2] => Test [3]

 

 

Im trying to always remove say the 4th word in quotations, or maybe the 3rd and 4th, whatever i want to omit i leave out from the echo, so echo $data[0].' '.$data[3].'; would output "This Test" omitting "Is A"

 

$data = 'hello world "this is a test"';
$data = str_word_count($data, 1);
echo $data[1].' '.$data[2].' '.$data[0];

 

Link to comment
Share on other sites

Kind of hard to really provide a solution based on the information provided. Can there be multiple instances of quoted text in the string? Is there text before AND after the quoted string(s)?

 

I would like to know more information before I invest time in writing any code. But, I think the approach I would take would be to use a regular expression to get the quoted text, split it using explode, remove the unwanted words and implode it back, and finally replace the original value with the modified text.

Link to comment
Share on other sites

Not sure but do you mean something like this?

<?php
$data = 'hello world "this is a test"';
$RegEx = '/"[^"]*"/i';
if (preg_match($RegEx, $data, $regs)) {
    $quote = str_word_count($regs[0], 1);
    unset($quote[1]); //remove IS
    $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}

echo $data;

 

return

hello world "this a test"
Link to comment
Share on other sites

Kind of hard to really provide a solution based on the information provided. Can there be multiple instances of quoted text in the string? Is there text before AND after the quoted string(s)?

 

I would like to know more information before I invest time in writing any code. But, I think the approach I would take would be to use a regular expression to get the quoted text, split it using explode, remove the unwanted words and implode it back, and finally replace the original value with the modified text.

 

Thanks for your response, ive been working all week on a solution but usually people dont understand what im trying to do... There is only one set of quotes in the string, and there is always text before and after the quotes. I've tried messing with Regex a bit, the basics are easy enough but when you get it the more complex expressions its really overwhelming... It would work to split the matched string by spaces though, i assume thats what you mean.

Link to comment
Share on other sites

So is mine close ?

 

yeah RegEx's are like that but the more you do the easier they become, then you tend to over then then LOL

 

Yeah i think your's is close im getting unexpected T_CONSTANT_ENCAPSED_STRING for your regex, im trying to figure out what quote is unescaped...

Link to comment
Share on other sites

Just attached a file (tested),

but here is the code

<?php
$data = 'hello world "this is a test" last test';
$RegEx = '/"[^"]*"/i';
if (preg_match($RegEx, $data, $regs)) {
    $quote = str_word_count($regs[0], 1);
    unset($quote[1]); //remove IS
    $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}

echo $data;

OUTPUT was:

hello world "this a test" last test

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Just attached a file (tested),

but here is the code

<?php
$data = 'hello world "this is a test" last test';
$RegEx = '/"[^"]*"/i';
if (preg_match($RegEx, $data, $regs)) {
    $quote = str_word_count($regs[0], 1);
    unset($quote[1]); //remove IS
    $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data);
}

echo $data;

OUTPUT was:

hello world "this a test" last test

 

I copied the code again, same problem, however the attached code runs fine. That works perfect, thanks so much for your help, you made it seem easy, ive had quite a few people try and not be able to help so i really appreciate it. Could i change unset($quote[1]); to strtoupper($quote[1]); to say make the second word all uppercase?

Link to comment
Share on other sites

I created a function that allows you to determine which words to retain within the quoted text based upon the index.

function removeQuotedWords($string, $retainIdxAry)
{
    $matched = preg_match('#"([^"]*)"#', $string, $quotedText);
    if($matched == 0) { return false; }
    $quotedWordsAry = explode(' ', $quotedText[1]);
    $replacWordsAry = array_intersect_key($quotedWordsAry, array_flip($retainIdxAry));
    $replaceTextStr = implode(' ', $replacWordsAry);
    return preg_replace('#"([^"]*)"#', "\"{$replaceTextStr}\"", $string);
}

$data = 'hello world "this is a test" after text'
echo removeQuotedWords($data, array(0,3));

//Output: hello world "this test" after text

Link to comment
Share on other sites

I created a function that allows you to determine which words to retain within the quoted text based upon the index.

function removeQuotedWords($string, $retainIdxAry)
{
    $matched = preg_match('#"([^"]*)"#', $string, $quotedText);
    if($matched == 0) { return false; }
    $quotedWordsAry = explode(' ', $quotedText[1]);
    $replacWordsAry = array_intersect_key($quotedWordsAry, array_flip($retainIdxAry));
    $replaceTextStr = implode(' ', $replacWordsAry);
    return preg_replace('#"([^"]*)"#', "\"{$replaceTextStr}\"", $string);
}

$data = 'hello world "this is a test" after text'
echo removeQuotedWords($data, array(0,3));

//Output: hello world "this test" after text

 

Awesome, i can actually use this is a different instance, i appreciate it! Thank you!!! :)

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.