How can I remove random amount of dots and get the last number? - php

I'm trying to strip all the dots and then get the number, and NAME[X] as an output.
My output is:
NAME..................................................................................................3
NAME2...................................................................................................24
NAME3...............................................................................................................................................5
NAME4.......................347
NAME5............................................................................................7
NAME6......................................................................9
I've tried something like this so far:
function introExcerpt($id = null, $introExcerptCut = null)
{
$fileInfo['intro'] = 'my string';
$introExcerpt = trim($fileInfo['intro']);
$lines = preg_split('/\r\n|\r|\n/', $introExcerpt);
$intro = '<div class="toc"><ul class="toc">';
for ($i = 0; $i < count($lines); $i++) {
// if (isset($lines[$i]) && substr(trim($lines[$i]), -1) !== '.') {
$intro.= $lines[$i].'<br />';
//}
}
$intro .= '</div></ul>';
return $intro;
}

Not sure exactly what your output should look like, but you may try just running preg_replace directly on the variable containing all lines:
$lines = preg_replace("/(NAME\d+)\.+(\d+)/", "$1[$2]", $lines);
This would generate the following output based on your sample input:
NAME[3]
NAME2[24]
NAME3[5]
NAME4[347]
NAME5[7]
NAME6[9]

You can use the following function:
function introExcerpt($str, $id = null, $introExcerptCut = null)
{
$fileInfo['intro'] = $str;
$introExcerpt = trim($fileInfo['intro']);
$lines = preg_split('/\r\n|\r|\n/', $introExcerpt);
$intro = '<div class="toc"><ul class="toc">';
for ($i = 0; $i < count($lines); $i++) {
$intro .= '<li>';
$tmpLineArray = explode('.', $lines[$i]);
array_filter($tmpLineArray, function ($value) {
return !is_null($value) && $value != '';
});
foreach ($tmpLineArray as $value) {
$intro .= $value . ' ';
}
$intro .= '</li>';
}
$intro .= '</ul></div>';
return $intro;
}
It is splitting the entire row into array using dot as separator and filters out the empty elements.

Related

PHP - echo the opening and closing of tags from array

Here I have an array and there is content situated inside it, their is either, one object, two, or more - depending on the tags required; the first element nested in the multidimensional array would be the textual output, unless it is an array, then the first element inside the array would be the text.
However, the other content in the array is in reference to the HTML tag they correspond to, such as:
[1] => Array
(
[0] => bo <====== Text to output
[1] => bold <====== tag to be within
)
However, in the module of simplicity, I would prefer for the content not to constantly repeat such responses, like:
This is a test <b>bo</b><i><b>ld</b></i><i>,</i> <u><i>u</i></u><u>nderline</u> ...
Instead the output should be:
This is a test<b>bo<i>ld</i></b><i>, <u>u</u></i><u>nderline</u> ...
This is the PHP code I have for it so far...
$use = array();
$base = "";
foreach ($build as $part => $data) {
// print_r($use);
if(!is_array($data)){
$base .= $data;
} else {
$text = array_shift($data);
if(!is_array($data[0])){
$data = array($data[0]);
} else {
$data = $data[0];
}
$removed = array_diff($use,$data);
foreach (($data) as $tag) {
if (in_array($tag, array_diff($use,$data))) {
$base .= "<\/" . $tag . ">";
} elseif(!in_array($tag, $use)){
$base .= "<" . $tag . ">";
array_push($use, $tag);
}
}
$use = $data;
$base .= $text;
}
}
print_r($base);
And here is the array if required (in JSON format!):
["This is a test\nIncluding ",["bo","bold"],["ld",["italic","bold"]],[", ","italic"],["u",["underline","italic"]],["nderlined","underline"],", ",["strike-through","strike"],", and ",["italic","italic"],"\ntext:\n\n",["numbered lists",["underline","strike","italic","bold"]],["\n",[]],"as well as",["\n",[]],["non ordered lists","http:\/\/test.com"],["\n",[]],"it works very well",["\n",[]],["try it","http:\/\/google.com"],"\n",["http:\/\/google.com",["bold","http:\/\/google.com"]],"\n\n",["wow","bold"],"\n",["lol","bold"]]
Any help would be much appreciated... thanks!
I'm honestly not sure if this is exactly what you're looking for. It would be great to have a full desired output... but I believe this is as close as it gets. It took me 3 hours so it better be it. It's a great question, very hard to accomplish.
I did print_r(htmlentities($base)), but you can simply do print_r($base) to see the formatted result. I did that because it was easier to check with the output you provided in the question.
Also, I modified your JSON because some tags specified there are non-existent. For example, I changed underline for u, italic for i, bold for b. Alternatives are em, strong... anyway, that's just a side-note.
<?php
$build = json_decode('["This is a test\nIncluding ",["bo","b"],["ld",["i","b"]],[", ","i"],["u",["u","i"]],["nderlined","u"],", ",["strike-through","strike"],", and ",["italic","i"],"\ntext:\n\n",["numbered lists",["u","strike","i","b"]],["\n",[]],"as well as",["\n",[]],["non ordered lists","http:\/\/test.com"],["\n",[]],"it works very well",["\n",[]],["try it","http:\/\/google.com"],"\n",["http:\/\/google.com",["b","http:\/\/google.com"]],"\n\n",["wow","b"],"\n",["lol","b"]]', true);
$used = [];
$base = '';
foreach($build as $data){
if(is_array($data)){
$text = array_shift($data);
$tags = $data[0];
if(!is_array($data[0])){
$tags = [$data[0]];
}
$elements = '';
$tagsToClose = array_diff($used, $tags);
$changes = true;
$i = 0;
foreach($tagsToClose as $tag){
while($changes){
$changes = false;
if($lastOpened != $tag){
$changes = true;
$elements .= '</'.$lastOpened.'>';
unset($used[$i++]);
$lastOpened = $used[$i];
}
}
$elements .= '</'.$tag.'>';
$key = array_search($tag, $used);
unset($used[$key]);
}
foreach($tags as $tag){
if(!in_array($tag, $used)){
$elements .= '<'.$tag.'>';
array_unshift($used, $tag);
$lastOpened = $tag;
}
}
$elements .= $text;
$data = $elements;
}
$base .= $data;
}
unset($used);
$base .= '</'.$lastOpened.'>';
print_r(htmlentities($base));
?>
EDIT
And here's the result I got, just in case you run into some trouble testing or to check with your results or whatever:
This is a test Including <b>bo<i>ld</i></b><i>, <u>u</u></i><u>nderlined, </u><strike>strike-through, and </strike><i>italic text: <u><strike><b>numbered lists</b></strike></u></i> as well as <http://test.com>non ordered lists</http://test.com> it works very well <http://google.com>try it <b>http://google.com </b></http://google.com><b>wow lol</b>
After many hours, this was my solution that I ended up with:
$build = json_decode('["This is a test Including\u00a0",["bo","bold"],["ld",["italic","bold"]],[",\u00a0","italic"],["u",["underline","italic"]],["nderlined,\u00a0","underline"],"strike-through, and\u00a0",["italic text:\u00a0","italic"],"it works very well\u00a0try it\u00a0",["http:\/\/google.com",["bold","http:\/\/google.com"]],["\u00a0wow lol","bold"]]',true);
$standard = array("bold"=>"b","underline"=>"u","strike"=>"s","italic"=>"i","link"=>"a","size"=>null);
$lists = array("ordered"=>"ol","bullet"=>"ul");
$size = array("huge"=>"2.5em","large"=>"1.5em");
$base = "";
foreach($build as $part){
$use = array();
$tags = true;
$len = 1;
if(!is_array($part) or count($part) == 1){
$text = $part;
$tags = false;
$part = array();
} else {
$text = array_shift($part);
if(count($part) == 1){
if(is_array($part[0])){
$part = $part[0];
}
}
if(!is_array($part)){
$part = array($part);
}
}
if($tags){
foreach ($part as $tag) {
if(!in_array($tag, array_keys($standard)) && !in_array($tag, array_keys($lists)) && !in_array($tag, array_keys($size))){
$base .= '<a href="' . $tag . '" title="' . $tag . '" class="link">';
$tag = "link";
} elseif(in_array($tag, array_keys($size))){
$base .= "<span style='font-size:" . $size[$tag] . "'>";
} elseif(!in_array($tag, array_keys($lists))) {
$base .= "<" . $standard[$tag] . ">";
}
array_push($use, $tag);
}
$base .= $text;
foreach (array_reverse($part) as $tag) {
if(!in_array($tag, array_keys($standard)) && !in_array($tag, array_keys($lists)) && !in_array($tag, array_keys($size))){
$base .= '</a>';
} elseif(in_array($tag, array_keys($size))){
$base .= "</span>";
} elseif (!in_array($tag, array_keys($lists))) {
$base .= "</" . $standard[$tag] . ">";
}
array_push($use, $tag);
}
} else {
$base .= $text;
}
}
print_r($base);

PHP look-and-say sequence

I'm trying to code Conway look-and-say sequence in PHP.
Here is my code:
function look_and_say ($number) {
$arr = str_split($number . " ");
$target = $arr[0];
$count = 0;
$res = "";
foreach($arr as $num){
if($num == $target){
$count++;
}else{
$res .= $count . $target;
$count = 1;
$target = $num;
}
}
return $res;
}
As I run the function, look_and_say(9900) I am getting value I expected: 2920.
My question is for assigning $arr to be $arr = str_split($number) rather than $arr = str_split($number . " "), the result omits the very last element of the $arr and return 29.
Is it normal to add empty space at the end of the $arr foreach to examine the last element or is there any better way to practice this code - besides regex way.
There are 2 methods I was able to come up with.
1 is to add concatenate at the result after the loop too.
function look_and_say ($number) {
$arr = str_split($number);
$target = $arr[0];
$count = 0;
$res = "";
foreach($arr as $num){
if($num == $target){
$count++;
}else{
$res .= $count . $target;
$count = 1;
$target = $num;
}
}
$res .= $count . $target;
return $res;
}
And the 2nd one is to add another if clause inside the loop and determine the last iteration:
function look_and_say ($number) {
$arr = str_split($number);
$target = $arr[0];
$count = 0;
$res = "";
$i=0;
$total = count($arr);
foreach($arr as $num){
if($i == ($total-1))
{
$count++;
$res .= $count . $target;
}
elseif($num == $target){
$count++;
}else{
$res .= $count . $target;
$count = 1;
$target = $num;
}
$i++;
}
return $res;
}
I want to suggest you other way using two nested while loops:
<?php
function lookAndSay($number) {
$digits = str_split($number);
$result = '';
$i = 0;
while ($i < count($digits)) {
$lastDigit = $digits[$i];
$count = 0;
while ($i < count($digits) && $lastDigit === $digits[$i]) {
$i++;
$count++;
}
$result .= $count . $lastDigit;
}
return $result;
}

PHP strings concatenation

My code cycles through a CSV file, converting it to XML:
<?php
for ($i = 1; $i < $arraySize; $i++) {
$n = 0;
if (substr($csv[$i][0], 0, 1) == $let) {
$surName = $dom->createElement('name');
$name = $csv[$i][0];
$nameText = $dom->createTextNode($name);
$surName->appendChild($nameText);
$text = str_replace(chr(94), ",", $csv[$i][4]);
$n = $i + 1;
$next = $csv[$n][0];
while ($next == 'NULL') {
$repl = str_replace(chr(94), ",", $csv[$n][4]);
$text = $repl;
$n++;
$next = $csv[$n][0];
}
$bio = $dom->createElement('bio');
$bioText = $dom->createTextNode($text);
$bio->appendChild($bioText);
$person = $dom->createElement('person');
$person->appendChild($surName);
$person->appendChild($bio);
$people->appendChild($person);
}
}
$xmlString = $dom->saveXML();
echo $xmlString;
?>
The problem is the $text = $repl; Typing $text .= $repl; brings:
error on line 1 at column 1: Document is empty.
but omitting the . just gives the last line of text.
the backup code works:
public function test($let){
$csv = $this->readCSV("data\AlphaIndex1M.csv");
$arraySize=sizeof($csv);
$let = strtoupper($let);
//echo '';
for($i=1; $i
echo $csv[$i][0];// .'
echo ', -->'.$csv[$i][4];
$n = $i+1;
$next = $csv[$n][0];
//if($next == 'NULL'){ }
while($next == 'NULL'){
echo $csv[$n][4]. " ";
$n++;
$next=$csv[$n][0];
}
//echo ''
echo '';
}
}
//echo ''
}
You have to initialize your $text before you can append stuff!
So write this before you use it:
$test = "";
(before the while loop or even before the for loop if you want all to be appended)

Multiple String Replace Based on Index

I need to replace multiple sections of a string based on their indices.
$string = '01234567890123456789';
$replacements = array(
array(3, 2, 'test'),
array(8, 2, 'haha')
);
$expected_result = '012test567haha0123456789';
Indices in $replacements are expected not to have overlaps.
I have been trying to write my own solution, split the original array into multiple pieces based on sections which needs to be replaced or not, and finally combine them:
echo str_replace_with_indices($string, $replacements);
// outputs the expected result '012test567haha0123456789'
function str_replace_with_indices ($string, $replacements) {
$string_chars = str_split($string);
$string_sections = array();
$replacing = false;
$section = 0;
foreach($string_chars as $char_idx => $char) {
if ($replacing != (($r_idx = replacing($replacements, $char_idx)) !== false)) {
$replacing = !$replacing;
$section++;
}
$string_sections[$section] = $string_sections[$section] ? $string_sections[$section] : array();
$string_sections[$section]['original'] .= $char;
if ($replacing) $string_sections[$section]['new'] = $replacements[$r_idx][2];
}
$string_result = '';
foreach($string_sections as $s) {
$string_result .= ($s['new']) ? $s['new'] : $s['original'];
}
return $string_result;
}
function replacing($replacements, $idx) {
foreach($replacements as $r_idx => $r) {
if ($idx >= $r[0] && $idx < $r[0]+$r[1]) {
return $r_idx;
}
}
return false;
}
Is there any more effective way to achieve the same result?
The above solution doesn't look elegant and feels quite long for string replacement.
Use this
$str = '01234567890123456789';
$rep = array(array(3,3,'test'), array(8,2,'haha'));
$index = 0;
$ctr = 0;
$index_strlen = 0;
foreach($rep as $s)
{
$index = $s[0]+$index_strlen;
$str = substr_replace($str, $s[2], $index, $s[1]);
$index_strlen += strlen($s[2]) - $s[1];
}
echo $str;

Recursive function - tree view - <ul> <li> ... (stuck)

It seems that I'm stuck with my recursive function.
I have a problem with closing the unnamed list (</ul>) and the list-items (</li>)
The thing what i get is
-aaa
-bbb
-b11
-b22
-b33
-ccc
-c11
-c22
-c33
-ddd
-d11
-d22
-d33
-eee
-fff
And the thing what i want is:
-aaa
-bbb
-b11
-b22
-b2a
-b2c
-b2b
-b33
-ccc
-c11
-c22
-c33
-c2a
-c2c
-c2c1
-c2c2
-c2b
-ddd
-d11
-d22
-d33
-eee
-fff
This is the code that i'm using
$html .= '<ul>';
$i = 0;
foreach ($result as $item)
{
$html .= "<li>$item->id";
$html .= getSubjects($item->id, NULL, "",$i); <--- start
$html .= "</li>";
}
$html .= '</ul>';
And the function
function getSubjects($chapter_id = NULL, $subject_id = NULL, $string = '', $i = 0 ) {
$i++;
// getting the information out of the database
// Depending of his parent was a chapter or a subject
$query = db_select('course_subject', 'su');
//JOIN node with users
$query->join('course_general_info', 'g', 'su.general_info_id = g.id');
// If his parent was a chapter - get all the values where chapter id = ...
if ($chapter_id != NULL) {
$query
->fields('g', array('short_title', 'general_id'))
->fields('su', array('id'))
->condition('su.chapter_id', $chapter_id, '=');
$result = $query->execute();
}
// if the parent is a subject -
// get value all the values where subject id = ...
else {
$query
->fields('g', array('short_title', 'general_id'))
->fields('su', array('id'))
->condition('su.subject_id', $subject_id, '=');
$result = $query->execute();
}
// Because count doesn't work (drupal)
$int = 0;
foreach ($result as $t) {
$int++;
}
// if there no values in result - than return the string
if ($int == 0) {
return $string;
}
else {
// Creating a new <ul>
$string .= "<ul>";
foreach ($result as $item) {
// change the id's
$subject_id = $item->id;
$chapter_id = NULL;
// and set the string --> with the function to his own function
$string .= "<li>$item->short_title - id - $item->id ";
getSubjects(NULL, $subject_id, $string, $i);
$string .="</li>";
}
$string .= "</ul>";
}
// I thougt that this return wasn't necessary
return $string;
}
Does someone have more experience with this kind of things?
All help is welcome.
I am not sure what you are trying to do but here is some code you can test and see if it helps to solve your problem:
This part is just for testing, it makes three dimensional array for testing:
for ($x = 0; $x < 2; $x++) {
$result["c$x"] = "ROOT-{$x}";
for ($y = 0; $y < 3; $y++) {
$result[$x]["c$y"] = "SECOND-{$x}-{$y}";
$rnd_count1 = rand(0,3);
for ($z = 0; $z < $rnd_count1; $z++) {
$result[$x][$y]["c$z"] = "RND-{$x}-{$y}-{$z}";
$rnd_count2 = rand(0,4);
for ($c = 0; $c < $rnd_count2; $c++) {
$result[$x][$y][$z][$c] = "LAST-{$x}-{$y}-{$z}-{$c}";
}
}
}
}
// $result is now four dimensional array with some values
// Last two levels gets random count starting from 0 items.
UPDATE:
Added some randomness and fourth level to test array.
And here is function which sorts array to unordered list:
function recursive(array $array, $list_open = false){
foreach ($array as $item) {
if (is_array($item)) {
$html .= "<ul>\n";
$html .= recursive($item, true);
$html .= "</ul>\n";
$list_open = false;
} else {
if (!$list_open) {
$html .= "<ul>\n";
$list_open = true;
}
$html .= "\t<li>$item</li>\n";
}
}
if ($list_open) $html .= "</ul>\n";
return $html;
}
// Then test run, output results to page:
echo recursive($result);
UPDATE:
Now it should open and close <ul> tags properly.

Categories