PHP new line array, look ahead - php

I have the following code routine, which is preg_match ing xml elements and printing the attributes of those elements.
However, in some of the tags, the content does not appear on one line (the SCRIPT tag), and therefore doesnt get matched.
I am wondering how to look ahead and gather all the lines until the closing tag "/>" ?
Is it possible to use the # character somewhere in the preg_match, to allow new lines?
I'm not even sure about how to go about resolving this. I've done a PHP sandbox so the code can be tested online:
http://sandbox.onlinephpfunctions.com/code/f96daef33fb49179eee30250ded81af6a8e5c567
If I remove all the data in the script tag, all apart from the first line, then it correctly outputs the array.
$file = ' <TOPTAG class="Menu" text="FCLPHP" >
<TAG1 name="contain=" />
<SCRIPT name="check()" script="if(B3||B4||B5 == 1){
do(ABC,0);
do(BCD,1);" />
</WINDOW>
';
//split the string into an array based on new line
$lines = explode("\n", $file);
//count the number of lines
$linesLength = count($lines);
for($index = 0; $index < $linesLength; $index++){
//reads all element atrributes from the TOPTAG element
$reads = element_attributes('TOPTAG',$lines[$index]);
//reads all element atrributes from the SCRIPT element
$scripts = element_attributes('SCRIPT',$lines[$index]);
//prints the script tag attributes
print_r($scripts);
}
function element_attributes($element_name, $xml) {
if ($xml == false) {
return false;
}
// Grab the string of attributes inside an element tag.
$found = preg_match('#<'.$element_name.
'\s+([^>]+(?:"|\'))\s?/?>#',
$xml, $matches);
if ($found == 1) {
$attribute_array = array();
$attribute_string = $matches[1];
// Match attribute-name attribute-value pairs.
$found = preg_match_all(
'#([^\s=]+)\s*=\s*(\'[^<\']*\'|"[^<"]*")#',
$attribute_string, $matches, PREG_SET_ORDER);
if ($found != 0) {
// Create an associative array that matches attribute
// names to attribute values.
foreach ($matches as $attribute) {
$attribute_array[$attribute[1]] =
substr($attribute[2], 1, -1);
}
return $attribute_array;
}
}
// Attributes either weren't found, or couldn't be extracted
// by the regular expression.
return false;
}

Your regexp operates across multiple lines. The problem is that you're only using it on one line at a time, so it never sees the continuation. Don't split the file into lines, just work with it as a single string.
$reads = element_attributes('TOPTAG',$file);
$scripts = element_attributes('SCRIPT',$file);

Related

How to remove, just once, a word that appears several times in the array

I've researched all sorts of ways, but I haven't found a solution for this case.
Basically I have to see if the word repeats and just remove the first occurrence of it in the array. For example:
$array_words = ['harmony', 'Acrobat', 'harmony', 'harmony'];
How do I check the repeated word, just once, leaving the array like this:
$array_final = ['Acrobat', 'harmony', 'harmony'];
I threw together this simple loop, and explained it with comments
$array_words = ['harmony', 'Acrobat', 'harmony', 'harmony'];
//get a count of each word in the array
$counted_values = array_count_values($array_words);
//hold the words we have already checked
$checked_words = [];
//variable to hold our output after filtering
$output = [];
//loop over words in array
foreach($array_words as $word) {
//if word has not been checked, and appears more than once
if(!in_array($word, $checked_words) && $counted_values[$word] > 1) {
//add word to checked list, continue to next word in array
$checked_words[] = $word;
continue;
}
//add word to output
$output[] = $word;
}
$output value
Array
(
[0] => Acrobat
[1] => harmony
[2] => harmony
)
GrumpyCrouton's solution is probably neater, but here's another way. Basically you put all the values into a single string, and then use string functions to do the work.
Code is commented with explanatory notes:
<?php
$array_words = ['harmony', 'Acrobat', 'harmony', 'harmony'];
$array_words_unique = array_unique($array_words); //get a list of unique words from the original array
$array_str = implode(",", $array_words);
foreach ($array_words_unique as $word) {
//count how many times the word occurs
$count = substr_count($array_str, $word);
//if it occurs more than once, remove the first occurence
if ($count > 1) {
//find the first position of the word in the string, then replace that with nothing
$pos = strpos($array_str, $word);
$array_str = substr_replace($array_str, "", $pos, strlen($word));
}
}
//convert back to an array, and filter any blank entries caused by commas with nothing between them
$array_final = array_filter(explode(",", $array_str));
var_dump($array_final);
Demo: https://3v4l.org/i1WKI
Credit to Using str_replace so that it only acts on the first match? for code to replace only the first occurence of a string inside another string.
We can use an array to keep track of each item that has been removed, and then use array_shift to move out of the item and count to limit loop overruns
<?php
$record = ['harmony','harmony', 'Acrobat', 'harmony', 'harmony','last'];
for($i=0,$count=count($record),$stack=array();$i<$count;$i++){
$item = array_shift($record);
in_array($item,$record) && !in_array($item,$stack)
? array_push($stack,$item)
: array_push($record,$item);
}
var_dump($record);

Search through array and removing duplicates

I am trying to remove duplicates from a text field. The text field auto suggests inputs and the user is only allowed to choose from them.
The user however has the option of choosing same input field more than once. It's an input fields that states the firstname + lastname of each individual from a database.
First, this is my code to trim some of the unwated characters and then going through the array comparing it to previous inputs.
if(!empty($_POST['textarea'])){
$text = $_POST['textarea'];
$text= ltrim ($text,'[');
$text= rtrim ($text,']');
$toReplace = ('"');
$replaceWith = ('');
$output = str_replace ($toReplace,$replaceWith,$text);
$noOfCommas = substr_count($output, ",");
echo $output.'<br>';
$tempArray = (explode(",",$output));
$finalArray[0] = $tempArray[0];
$i=0;
$j=0;
$foundMatch=0;
for ($i; $i<$noOfCommas; $i++) {
$maxJ = count($finalArray);
for ($j; $j<$maxJ; $j++) {
if ($tempArray[$i] === $finalArray[$j]) {
$foundMatch ===1;
}
}
if ($foundMatch === 0) {
array_push($finalArray[$j],$tempArray[$i]);
}
}
What is it am I doing wrong ?
In this part when checking if the values are equal:
if ($tempArray[$i] === $finalArray[$j]) {
$foundMatch ===1;
}
It should be:
if ($tempArray[$i] === $finalArray[$j]) {
$foundMatch = 1;
}
That way you are setting the variable and not checking if it's equal to 1. You can also break the inner for loop when finding the first match.
I think that this should work:
if (!empty($_POST['textarea'])){
$words = explode(',',str_replace('"', '', trim($_POST['textarea'], ' \t\n\r\0\x0B[]'));
array_walk($words, 'trim');
foreach ($words as $pos=>$word){
$temp = $words;
unset($temp[$pos]);
if (in_array($word, $temp))
unset($words[$pos]);
}
}
echo implode("\n", $words);
First it reads all the words from textarea, removes '"' and then trim. After that it creates a list of words(explode) followed by a trim for every word.
Then it checks every word from the list to see if it exists in that array (except for that pos). If it exists then it will remove it (unset).

Split an array with a regular expression

I'm wondering if it is possible to truncate an array by using a regular expression.
In particular I have an array like this one:
$array = array("AaBa","AaBb","AaBc","AaCa","AaCb","AaCc","AaDa"...);
I have this string:
$str = "AC";
I'd like the slice of $array from the start to the last occurrence of a string matching /A.C./ (in the sample, "AaCc" at index 5):
$result = array("AaBa","AaBb","AaBc","AaCa","AaCb","AaCc");
How can I do this? I thought I might use array_slice, but I don't know how to use a RegEx with it.
Here's my bid
function split_by_contents($ary, $pattern){
if (!is_array($ary)) return FALSE; // brief error checking
// keep track of the last position we had a match, and the current
// position we're searching
$last = -1; $c = 0;
// iterate over the array
foreach ($ary as $k => $v){
// check for a pattern match
if (preg_match($pattern, $v)){
// we found a match, record it
$last = $c;
}
// increment place holder
$c++;
}
// if we found a match, return up until the last match
// if we didn't find one, return what was passed in
return $last != -1 ? array_slice($ary, 0, $last + 1) : $ary;
}
Update
My original answer has a $limit argument that served no purpose. I did originally have a different direction I was going to go with the solution, but decided to keep it simple. However, below is the version that implements that $limit. So...
function split_by_contents($ary, $pattern, $limit = 0){
// really simple error checking
if (!is_array($ary)) return FALSE;
// track the location of the last match, the index of the
// element we're on, and how many matches we have found
$last = -1; $c = 0; $matches = 0;
// iterate over all items (use foreach to keep key integrity)
foreach ($ary as $k => $v){
// text for a pattern match
if (preg_match($pattern, $v)){
// record the last position of a match
$last = $c;
// if there is a specified limit, capture up until
// $limit number of matches, then exit the loop
// and return what we have
if ($limit > 0 && ++$matches == $limit){
break;
}
}
// increment position counter
$c++;
}
I think the easiest way might be with a foreach loop, then using a regex against each value - happy to be proven wrong though!
One alternative could be to implode the array first...
$array = array("AaBa","AaBb","AaBc","AaCa","AaCb","AaCc","AaDa"...);
$string = implode('~~',$array);
//Some regex to split the string up as you want, guessing something like
// '!~~A.C.~~!' will match just what you want?
$result = explode('~~',$string);
If you'd like a hand with the regex I can do, just not 100% on exactly what you're asking - the "A*C*"-->"AaCc" bit I'm not too sure on?
Assuming incremental numeric indices starting from 0
$array = array("AaBa","AaBb","AaBc","AaCa","AaCb","AaCc","AaDa");
$str = "AC";
$regexpSearch = '/^'.implode('.',str_split($str)).'.$/';
$slicedArray = array_slice($array,
0,
array_pop(array_keys(array_filter($array,
function($entry) use ($regexpSearch) {
return preg_match($regexpSearch,$entry);
}
)
)
)+1
);
var_dump($slicedArray);
PHP >= 5.3.0 and will give a
Strict standards: Only variables should be passed by reference
And if no match is found, will still return the first element.

Read a .info file with PHP

I've created a .info file similar to how you would in drupal.
#Comment
Template Name = Valley
styles[] = styles/styles.css, styles/media.css
scripts[] = js/script.js
I want to use PHP get each variable and their values. For example I'd like to put the Template Name value to a PHP variable called Template Name and put the styles[] values in an array if there is mroe than one.
I'd also need to avoid it picking up on comments that are defined be a hash # before the text.
It seems a lot to ask, bt I'm really not sure how to go about doing this. If someone has a solution I'd be very greatful, however if someone could just point me in the right direction that'll be just as helpful.
Thanks in advanced!
If you can adkust your info file slightly, you can use a built-in PHP function:
http://php.net/manual/en/function.parse-ini-file.php
#Comment
TemplateName = Valley
styles[] = "styles/styles.css"
styles[] = "styles/media.css"
scripts[] = "js/script.js"
which will result in an array
If all you're after is something "similar" you could take a look at the parse_ini_file() function.
Drupal was a good hint:
function drupal_parse_info_file($filename) {
$info = array();
$constants = get_defined_constants();
if (!file_exists($filename)) {
return $info;
}
$data = file_get_contents($filename);
if (preg_match_all('
#^\s* # Start at the beginning of a line, ignoring leading whitespace
((?:
[^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
\[[^\[\]]*\] # unless they are balanced and not nested
)+?)
\s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
(?:
("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
(\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
([^\r\n]*?) # Non-quoted string
)\s*$ # Stop at the next end of a line, ignoring trailing whitespace
#msx', $data, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
// Fetch the key and value string
$i = 0;
foreach (array('key', 'value1', 'value2', 'value3') as $var) {
$$var = isset($match[++$i]) ? $match[$i] : '';
}
$value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
// Parse array syntax
$keys = preg_split('/\]?\[/', rtrim($key, ']'));
$last = array_pop($keys);
$parent = &$info;
// Create nested arrays
foreach ($keys as $key) {
if ($key == '') {
$key = count($parent);
}
if (!isset($parent[$key]) || !is_array($parent[$key])) {
$parent[$key] = array();
}
$parent = &$parent[$key];
}
// Handle PHP constants.
if (isset($constants[$value])) {
$value = $constants[$value];
}
// Insert actual value
if ($last == '') {
$last = count($parent);
}
$parent[$last] = $value;
}
}
return $info;
}
Source, this function is part of the drupal code-base, drupal's license applies, used for documentation purposes here only.

CSV Import Split by Comma - what to do about quotes?

I have a CSV file I'm importing but am running into an issue. The data is in the format:
TEST 690, "This is a test 1, 2 and 3" ,$14.95 ,4
I need to be able to explode by the , that are not within the quotes...
See the fgetcsv function.
If you already have a string, you can create a stream that wraps it and then use fgetcsv. See http://code.google.com/p/phpstringstream/source/browse/trunk/stringstream.php
If you really want to do this by hand, here's a rough reference implementation I wrote to explode a complete line of CSV text into an array. Be warned: This code does NOT handle multiple-line fields! With this implementation, the entire CSV row must exist on a single line with no line breaks!
<?php
//-----------------------------------------------------------------------
function csvexplode($str, $delim = ',', $qual = "\"")
// Explode a single CSV string (line) into an array.
{
$len = strlen($str); // Store the complete length of the string for easy reference.
$inside = false; // Maintain state when we're inside quoted elements.
$lastWasDelim = false; // Maintain state if we just started a new element.
$word = ''; // Accumulator for current element.
for($i = 0; $i < $len; ++$i)
{
// We're outside a quoted element, and the current char is a field delimiter.
if(!$inside && $str[$i]==$delim)
{
$out[] = $word;
$word = '';
$lastWasDelim = true;
}
// We're inside a quoted element, the current char is a qualifier, and the next char is a qualifier.
elseif($inside && $str[$i]==$qual && ($i<$len && $str[$i+1]==$qual))
{
$word .= $qual; // Add one qual into the element,
++$i; // Then skip ahead to the next non-qual char.
}
// The current char is a qualifier (so we're either entering or leaving a quoted element.)
elseif ($str[$i] == $qual)
{
$inside = !$inside;
}
// We're outside a quoted element, the current char is whitespace and the 'last' char was a delimiter.
elseif( !$inside && ($str[$i]==" ") && $lastWasDelim)
{
// Just skip the char because it's leading whitespace in front of an element.
}
// Outside a quoted element, the current char is whitespace, the "next" char is a delimiter.
elseif(!$inside && ($str[$i]==" ") )
{
// Look ahead for the next non-whitespace char.
$lookAhead = $i+1;
while(($lookAhead < $len) && ($str[$lookAhead] == " "))
{
++$lookAhead;
}
// If the next char is formatting, we're dealing with trailing whitespace.
if($str[$lookAhead] == $delim || $str[$lookAhead] == $qual)
{
$i = $lookAhead-1; // Jump the pointer ahead to right before the delimiter or qualifier.
}
// Otherwise we're still in the middle of an element, so add the whitespace to the output.
else
{
$word .= $str[$i];
}
}
// If all else fails, add the character to the current element.
else
{
$word .= $str[$i];
$lastWasDelim = false;
}
}
$out[] = $word;
return $out;
}
// Examples:
$csvInput = 'Name,Address,Phone
Alice,123 First Street,"555-555-5555"
Bob,"345 Second Place, City ST",666-666-6666
"Charlie ""Chuck"" Doe", 3rd Circle ," 777-777-7777"';
// explode() emulates file() in this context.
foreach(explode("\n", $csvInput) as $line)
{
var_dump(csvexplode($line));
}
?>
I would still recommend relying on PHP's built-in function though. That's (hopefully) going to be far more reliable long term. Artefacto and Roadmaster are right.: anything you have to do to the data is best done after you import it.

Categories