PHP Find two consecutive numeric entries in array - php

My problem is it just replicates the number twice, though it does add the break when there is a number, but I'm trying to check if there is a number after the number, so it would say line 12 is.....
Thanks for any help
<?PHP
$lines = file_get_contents('http://www.webstitcher.com/test.txt');
$tag = str_split($lines); // puts all lines into a array
foreach ($tag as $num => $letta){
if (is_numeric($letta) == TRUE){
$num2 = $num++;
if (is_numeric($tag[$num2])){ // checks if next line is going to be another digit
$letta .= $tag[$num2];
unset($tag[$num2]); // removes line if it had another digit and adds to ouput
}
echo '<br />' . $letta;
}
else {
echo $letta;
}
}
?>

Try exploding the string using ' ' as the delimiter. This will allow you to keep the numbers whole and will ultimately help cut out a lot of the complexity.
$lines = file_get_contents('http://www.webstitcher.com/test.txt');
$tag = explode(' ', $lines); // puts all words into a array
foreach ($tag as $word){
if (is_numeric($word)) {
// if the word is numeric, simply skip to next line
// if you need to keep the number, add $word to the echo statement
echo '<br />';
}
else {
echo ' '.$word;
}
}
This way you don't have to keep track of the previous element in the array or check for the next element.
Alternatively, you could also use preg_replace which would remove the need for the loop entirely.
$lines = preg_replace('/[0-9]+/', '<br>', $words);

Related

Php- Eliminate characters while reading file

I would I am using PHP. I am reading from a file but I would like to eliminate the following characters from the file wherever they are: '' and { }.
I tried to use the trim function but the characters "',{ and }" are still present in the output:
$txt_file = file_get_contents('out.txt');
$rows = explode(",", $txt_file);
array_shift($rows);
foreach($rows as $row => $data)
{
//get row data
$row_data = explode(':', $data);
trim($row,"'");
trim($row,"{");
trim($row,"}");
$info[$row]['state'] = $row_data[0];
$info[$row]['action'] = $row_data[1];
echo $info[$row]['state'] . '<br />';
echo $info[$row]['action'] . '<br />';
echo '<br />';
}
Do you have any idea how to do it?
Thanks
I assume you want to remove ' { and } from $row
If it is, then replace
trim($row,"'");
trim($row,"{");
trim($row,"}");
with
$row = str_replace(['\'', '{', '}'], '', $row);
Note: trim — Strip whitespace (or other characters) from the beginning and end of a string. Moreover you didn't store trimmed data to any variable so literally you will not get trimmed data. You want to replace characters wherever it is found so use str_replace or preg_replace. Please check PHP manual for more details

adding link values around each string

I have a php value coming back from my database as a string, like
"this, that, another, another"
And I'm trying to wrap a separate link around each of those strings, but I can't seem to get it to work. I've tried a for loop, but since it's just a string of information and not an array of information that doesn't really work. Is there a way to wrap a unique link around each value in my string?
The easiest way that I see to do this would be using PHP's explode() function. You'll find that it will become very useful as you start to use PHP more and more, so do check out its documentation page. It allows you to split a string up into an array given a certain separator. In your case, this would be ,. So to split the string:
$string = 'this, that, another, another 2';
$parts = explode(', ', $string);
Then use a foreach (again, check the documentation) to iterate through each of the parts and make them into a link:
foreach($parts as $part) {
echo '' . $part . "\n";
}
However, you can do this with a for loop. Strings can be accessed like arrays, so you can implement a parser pattern to parse the string, extract the parts, and create the links.
// Initialize some vars that we'll need
$str = "this, that, another, another";
$output = ""; // final output
$buffer = ""; // buffer to hold current part
// Iterate over each character
for($i = 0; $i < strlen($str); $i++) {
// If the character is our separator
if($str[$i] === ',') {
// We've reached the end of this part, so add it to our output
$output .= '' . trim($buffer) . "\n";
// clear it so we can start storing the next part
$buffer = "";
// and skip to the next character
continue;
}
// Otherwise, add the character to the buffer for the current part
$buffer .= $str[$i];
}
echo $output;
(Codepad Demo)
A better way is to do it like this
$string = "this, that, another, another";
$ex_string = explode(",",$string);
foreach($ex_string AS $item)
{
echo "<a href='#'>".$item."</a><br />";
}
First explode the string to get the individual words in an array. Then add the hyperlinks to the words and finally implode them.
$string = "this, that, another, another";
$words = explode(",", $string);
$words[0] = $words[0]
$words[1] = $words[1]
..
$string = implode(",", $words);
You can also use the for loop to assign hyperlinks that follow a pattern like this:
for ($i=0; $i<count($words); $i++) {
//assign URL for each word as its name or index
}

Trying to spit sentence into array with words

I am trying to split a sentence into an array with words, one word as each element, in PHP if there is more than one word in the sentence. If there is only one word in the sentence, then I just print that one word.
My issue is when I split the sentence into words delimited by a space and put the contents into an array. I do this all using explode. But when I run through the array that explode apparently makes, it says there is nothing in the array when I try to print each item.
Here is my code:
if(isset($_GET['check'])){
$input = trim($_GET['check']);
$sentence='';
if(stripos($input, ' ')!==false){
$sentence = explode(' ', $input);
foreach($sentence as $item){
echo $item;
}
}
else{
echo $input;
}
}
Why is echo $item; printing nothing? Why isn't there anything in the array $sentence?
Your code seems to be working fine. Make sure you're getting the variable. You can do a print_r($_GET);
You can also just do this:
<?php
$_GET['check'] = 'Hey how are you?';
if (isset($_GET['check'])) {
// each word is now an element in the array
$arr = explode(' ', trim($_GET['check']));
}
// piece each word back together with a space
echo implode(' ', $arr);
?>
It doesn't matter if it's a single word or multiple words.
UPDATE: If you really want to check if the user has entered one word or more than one word you can do this:
<?php
$_GET['check'] = 'Hey how are you?';
if (str_word_count($_GET['check']) > 1) {
echo 'More than one word';
} else {
echo 'Only one word';
}
?>
Check out str_word_count

echo partial text

I want to display just two lines of the paragraph.
How do I do this ?
<p><?php if($display){ echo $crow->content;} ?></p>
Depending on the textual content you are referring to, you might be able to get away with this :
// `nl2br` is a function that converts new lines into the '<br/>' element.
$newContent = nl2br($crow->content);
// `explode` will then split the content at each appearance of '<br/>'.
$splitContent = explode("<br/>",$newContent);
// Here we simply extract the first and second items in our array.
$firstLine = $splitContent[0];
$secondLine = $splitContent[1];
NOTE - This will destroy all the line breaks you have in your text! You'll have to insert them again if you still want to preserve the text in its original formatting.
If you mean sentences you are able to do this by exploding the paragraph and selecting the first two parts of the array:
$array = explode('.', $paragraph);
$2lines = $array[0].$array[1];
Otherwise you will have to count the number of characters across two lines and use a substr() function. For example if the length of two lines is 100 characters you would do:
$2lines = substr($paragraph, 0, 200);
However due to the fact that not all font characters are the same width it may be difficult to do this accurately. I would suggest taking the widest character, such as a 'W' and echo as many of these in one line. Then count the maximum number of the largest character that can be displayed across two lines. From this you will have the optimum number. Although this will not give you a compact two lines, it will ensure that it can not go over two lines.
This is could, however, cause a word to be cut in two. To solve this we are able to use the explode function to find the last word in the extracted characters.
$array = explode(' ', $2lines);
We can then find the last word and remove the correct number of characters from the final output.
$numwords = count($array);
$lastword = $array[$numwords];
$numchars = strlen($lastword);
$2lines = substr($2lines, 0, (0-$numchars));
function getLines($text, $lines)
{
$text = explode("\n", $text, $lines + 1); //The last entrie will be all lines you dont want.
array_pop($text); //Remove the lines you didn't want.
return implode("<br>", $text); //Implode with "<br>" to a string. (This is for a HTML page, right?)
}
echo getLines($crow->content, 2); //The first two lines of $crow->content
Try this:
$lines = preg_split("/[\r\n]+/", $crow->content, 3);
echo $lines[0] . '<br />' . $lines[1];
and for variable number of lines, use:
$num_of_lines = 2;
$lines = preg_split("/[\r\n]+/", $crow->content, $num_of_lines+1);
array_pop($lines);
echo implode('<br />', $lines);
Cheers!
This is a more general answer - you can get any amount of lines using this:
function getLines($paragraph, $lines){
$lineArr = explode("\n",$paragraph);
$newParagraph = null;
if(count($lineArr) > 0){
for($i = 0; $i < $lines; $i++){
if(isset($lines[$i]))
$newParagraph .= $lines[$i];
else
break;
}
}
return $newParagraph;
}
you could use echo getLines($crow->content,2); to do what you want.

php: replacing double <br /> with </p><p>

i use nicEdit to write RTF data in my CMS. The problem is that it generates strings like this:
hello first line<br><br />this is a second line<br />this is a 3rd line
since this is for a news site, i much prefer the final html to be like this:
<p>hello first line</p><p>this is a second line<br />this is a 3rd line</p>
so my current solution is this:
i need to trim the $data for <br /> at the start/end of the string
replace all strings that have 2 <br/> or more with </p><p> (one single <br /> is allowed).
finally, add <p> at the start and </p> at the end
i only have steps 1 and 3 so far. can someone give me a hand with step 2?
function replace_br($data) {
# step 1
$data = trim($data,'<p>');
$data = trim($data,'</p>');
$data = trim($data,'<br />');
# step 2 ???
// preg_replace() ?
# step 3
$data = '<p>'.$data.'</p>';
return $data;
}
thanks!
ps: it would be even better to avoid specific situations. example: "hello<br /><br /><br /><br /><br />too much space" -- those 5 breaklines should also be converted to just one "</p><p>"
final solution (special thanks to kemp!)
function sanitize_content($data) {
$data = strip_tags($data,'<p>,<br>,<img>,<a>,<strong>,<u>,<em>,<blockquote>,<ol>,<ul>,<li>,<span>');
$data = trim($data,'<p>');
$data = trim($data,'</p>');
$data = trim($data,'<br />');
$data = preg_replace('#(?:<br\s*/?>\s*?){2,}#','</p><p>',$data);
$data = '<p>'.$data.'</p>';
return $data;
}
This will work even if the two <br>s are on different lines (i.e. there is a newline or any whitespace between them):
function replace_br($data) {
$data = preg_replace('#(?:<br\s*/?>\s*?){2,}#', '</p><p>', $data);
return "<p>$data</p>";
}
This approach will solve your problem:
Split the string on <br> or <br />: you'll get an array of strings.
Create a new string <p>.
Loop on the array of 1, from the beginning to the end and remove all entries that are empty, until an entry that is not empty (break).
Same as 3, but from the end to the beginning of the array.
Loop on the array of 1, have an integer value A (default 0), which states that there is a single or double break.
If the string is empty, increase the value of A and continue the loop.
If the string is not empty:
If the value of A is 1 or below, append a <br>.
If the value of A is 2 or above, append a </p><p>.
Append the content of the current entry (which is not empty).
Set the value of A to 0.
Append </p>
A different approach: using Regular Expressions
(<br ?/?>){2,}
Will match 2 or more <br>. (See php.net on preg_split on how to do this.)
Now, the same approach on step 2 and 3: loop on the array twice, once from the beginning up (0..length) and once from the end down (length-1..0). If the entry is empty, remove it from the array. If the entry is not empty, quit the loop.
To do this:
$array = preg_split('/(<br ?/?>\s*){2,}/i', $string);
foreach($i = 0; $i < count($array); $i++) {
if($value == "") {
unset($array[$i]);
}else{
break;
}
}
foreach($i = count($array) - 1; $i >= 0; $i--) {
if($value == "") {
unset($array[$i]);
}else{
break;
}
}
$newString = '<p>' . implode($array, '</p><p>') . '</p>';
I think this should work for step #2 unless I am not understanding your scenario completely:
$string = str_replace( '<br><br>', '</p><p>', $string );
$string = str_replace( '<br /><br />', '</p><p>', $string );
$string = str_replace( '<br><br />', '</p><p>', $string );
$string = str_replace( '<br /><br>', '</p><p>', $string );

Categories