Jump to content

Extracting quotes with regular expressions


kickstart

Recommended Posts

Hi

 

Trying to work out how to do this, but I am very weak at regular expressions.

 

If I have a string as follows:-

 

The quick "brown fox" jumped over the "lazy dog"

 

I would like to extract the quotes strings (ie brown fox and lazy dog)

 

Think I can do this with the following:-

 

preg_match_all( '/".*?"/', 'The quick "brown fox" jumped over the "lazy dog"', $matches )

 

which will put each of the quoted strings into an array.

 

However I would also like a way to extract the rest of the string separately - preferably without looping round the extracted array.

 

Also, if I wanted to cope with strings enclosed in single quotes as well as double quotes, how can I expand it to cover that?

 

Thank you

 

All the best

 

Keith

 

 

Link to comment
Share on other sites

non-regex solution

 

<?php

$str = 'The quick "brown fox" jumps over the "lazy dog".';

$result = xQuotes($str);

echo '<pre>'.print_r($result, 1).'</pre>';

function xQuotes($str) 
{
    $k = strlen($str);
    $i = $j = 0;
    $res = array();
    $inquotes = false;
    for ($i=0; $i<$k; ++$i) {
        switch($c = $str[$i]) {
            case '"':
                if (!$inquotes) ++$j;
                $inquotes = !$inquotes;
                break;
            default:
                $index = $inquotes ? $j : 0;
                if (isset($res[$index])) 
                    $res[$index] .= $c;
                else
                    $res[$index] = $c;
                break;       
        }
    }
    return $res;
}
?>

 

results==>

Array

(

    0 => The quick  jumps over the .

    1 => brown fox

    2 => lazy dog

)

Link to comment
Share on other sites

Hi

 

Cheers. I thought a regex would have been the quickest, but it is a non trivial one it would seem.

 

I will have a play with Barands solution, but modify it to cope with single quote delimited strings as well as double quote delimited strings.

 

As an aside '/(".*?")|(\'.*?\')/' works to extract single and double quoted strings (just not found a way to extract things other than those).

 

Thank you

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Played with Barands script to cope with both single and double quoted strings.

 

function xQuotes($str) 
{
$str_array = str_split($str);
$j = 0;
    $res = array();
    $insinglequotes = false;
    $indoublequotes = false;
foreach($str_array AS $str_item)
{
        switch(true) 
	{
            case $str_item == "'" AND !$indoublequotes:
			if (!$insinglequotes) ++$j;
			$insinglequotes = !$insinglequotes;
			break;
            case $str_item == '"' AND !$insinglequotes:
			if (!$indoublequotes) ++$j;
			$indoublequotes = !$indoublequotes;
                break;
            default:
                $index = (($insinglequotes || $indoublequotes) ? $j : 0);
                if (isset($res[$index])) 
                    $res[$index] .= $str_item;
                else
                    $res[$index] = $str_item;
                break;       
        }
    }
if ($insinglequotes || $indoublequotes)
{
	$res[0] .= $res[$j];
	unset($res[$j]);
}
    return $res;
}

 

All the best

 

Keith

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.