I'm using this string to get what's needed:
echo "<div id=\"lot\">".$table->children(2)->children(0)->plaintext."</div>";
And I get the result: 01 02 03 04 05 06 10 15 17 27 31 etc.
What I need is to add <div id=\"new\"></div> to each number.
<div id=\"new\">01</div> <div id=\"new\">02</div> etc etc.
I tried to use the code below but it doesn't work
$table = $box_rezult->find("table.last-d", 0);
$results = $table->children(2)->children(0)->plaintext;
$string = $results;
$var1 = str_replace(" ", "<div id=\"new\"> </div>", $string);
echo $var1;
I would appreciate any help, thanks!
Thanks to everyone!!
explode() the string to an array, then implode() it:
echo '<div class="new">' . implode('</div><div class="new">', explode(' ', $string)) . '</div>';
I've changed id to class as id's must be unique.
Demo
For your use case you need to flip tags to get a separator and pre- and append one more:
$var1 = "<div id=\"new\">" . str_replace(" ", "</div> <div id=\"new\">", $string) . "</div>";
Assuming that $string contains all the numbers you want,
you can explode it and put each element you want to have in tags for each number and then apply it to string, using a loop (or implode, as suggested):
$numbers = explode(" ", $string);
$var1 = "";
foreach($numbers as $number){
$var1 .= "<div id=\"new\">".$number."</div>";
}
Also, please take into consideration about id attribute uniqueness. Use class attribute instead.
Related
I have a text area that gets input from a PHP script, and I would like to count the number of words or line breaks in the textarea and echo it below the text area.
This is what the code looks like for the textarea.
<textarea name="domains" cols="120" rows="5" style="max-width:100%;">
<?php $output_array = explode(" ",$output);
$count = count($output_array);
for ($i=0;$i<$count;$i++){
echo $output_array[$i]."\n";
}
?>
</textarea><br />
<br>
<?php
preg_match_all("/(\n)/", $_POST['domains'], $matches);
$total_lines = count($matches[0]) + 1;
echo $total_lines;
?>
<br />
I tried using preg_match_all but the output I get is only "1", regardless of how many line breaks are inside the text area.
$str = 'as aa frd sad as
kjhsdf sdkjh
sd sdkjhsdf
sjkldhfh sdfjh sd';
preg_match_all("/\w+/", $str, $matches);
echo 'Words = ' . count($matches[0]);
echo PHP_EOL;
preg_match_all("/\n/", $str, $matches);
echo 'newlines = ' . count($matches[0]);
echo PHP_EOL;
echo 'So number of line is = ' . count($matches[0])+1;
RESULT
Words = 12
newlines = 3
So number of line is = 4
You can do the following:
$arr = array_filter(explode(" ", $input) function($item) {
return !!$item;
});
$count = count($arr) + count(explode("\n", implode(",", $arr)));
This splits the string by space, filters out empty items to avoid issues due to multiple spaces found between words, then glues the array back into a string, splits it by newline and adds the two counts together.
Let say I have a string
$content = "hello . wow . cool . yes!";
It's easy to just replace the dot to another string:
echo preg_replace("/\./", "<img>", $content);
so the output is:
hello <img> wow <img> cool <img> yes!
however, is there any way to replace it one by one?
for example, I have an array and would like to insert them into the string.
$arr = ['<img src="a"/>', '<img src="b"/>', '<img src="c"/>'];
the expected output:
hello <img src="a"/> wow <img src="b"/> cool <img src="c"/> yes!
I can use preg_match_all to get the separator but still have no idea how to replace it respectively.
Thank you for any help.
Use preg_replace_callback. Store the number of the current full stop in a static variable. Access $arr from the callback with use (). Use modulo in case there are not enough items in $arr.
<?php
$content = 'hello . wow . cool . yes!';
$arr = ['<img src="a"/>', '<img src="b"/>', '<img src="c"/>'];
$content = preg_replace_callback ('/\./', function ($_) use ($arr) {
static $count = 0;
return $arr [$count++ % count ($arr)];
}, $content);
echo ($content);
http://sandbox.onlinephpfunctions.com/code/f9115bdb6eb17e30012d987ed1fc4b95e7c10d33.
I have this kind of text file.
"u901_humext ""2019-02-01 23:24"" 99.74 99.9 99.82".
And I want to read line one by one and put into a variable like:
name = u901_humext
date = 2019-02-01 23:24
min = 99.74
But I don't know how to split it.
You can do this as below:
$string = '"u901_humext ""2019-02-01 23:24"" 99.74 99.9 99.82"';
$explodedArray = explode('"', $string);
foreach($explodedArray as $key => $element){
$explodedArray[$key] = trim($element, ' "');
}
echo "Name: ".$explodedArray[0]."\n";
echo "Date: ".$explodedArray[1]."\n";
echo "Min: ".$explodedArray[2];
Explanation:
Firstly split the string using explode() function of PHP.
Then loop through each element of the array. Trim each element of the array to remove "(double quote) and (space).
Print the elements
I had an value in database like "demo text" . I want to display this content from the db in the view page as
Html code that i am using is like this <h2>Demo<span>Text</span></h2> , is there any solution for seperate each words and use one for h2 and other for span. I am using php codeigniter for the project , I don't know that whether the way i explained my problem is correct or not .
Yes you can so it with explode
if you have stored at least 2 words with space. try following
$demo ="demo text";
$arr = explode(" ",$demo);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
EDIT
If you have more words and want to split first word only you can pass limit parameter in explode
$demo ="Pligrimage to Marian Shrines";
$arr = explode(" ",$demo,2);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
I think you are looking for something like this!
$your_string = "Hello Houston! We have a problem!";
$my_array = explode(" ",trim($your_string));
$output = "<h2>";
foreach($my_array as $a_word){
if ($a_word === reset($my_array))
$output .= $a_word;
else
$output .= " <span>". $a_word . "</span>";
}
$output .= "</h2>";
print $output;
In the following code, I need to remove <div class="grid_8"></div>.
They will always be at the start and finish of my string, for example:
<div class="grid_8"><img src="http://rps.sanscode.com/site/assets/media/images/rps_mini_logo.png" border="0" alt="Rapid Print Solutions" style="margin-bottom: 30px;" />
<h1></h1>
</div>
What is a suitable regex for preg_replace to remove it? 8 can be any number between 1 and 16.
Thanks
Jason
#Amjad...
Here is my code
public function fix_grid(){
$result = db::query("select * from sc_content_components where component_value_1 like '%grid_%'")->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $item){
$json = json_decode($item['component_value_1']);
if(is_null($json)) continue;
$x = reset($json);
echo htmlspecialchars($x);
echo "<p>=======================<b>Changes to: </b></p>";
$patterns = array('/^<(div)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:\'[^\']*\')|[^>\s]+))?)*)\s*(\/?)>/'
, '/<\/(\div+)[^>]*>$/');
$x = preg_replace($patterns, array('',''), trim($x));
echo htmlspecialchars($x);
echo "<hr>";
$json[0]=$x;
// $ne['component_value_1'] = json_encode($json);
// db::where('component_id', $item['component_id']);
// db::update('sc_content_component', $ne);
}
}
I'm using the regex below (#Amjad Masad) and it doesn't remove the last div.
As you can see I am using trim and it doesn't seem to work
For the stated problem, this is the solution:
Edit: expanded regex =
/^\s*<div\s (?:".*?"|\'.*?\'|[^>]*?)*
(?<=\s)class\s*=\s*(["\'])\s*grid_(?:1[0-6]|[1-9])\s*\1
(?:".*?"|\'.*?\'|[^>]*?)*
>
(.*)
<\/div\s*>
\s*$/xs
replacement = "$2"
For the open
$patterns = array('/^<(div)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:\'[^\']*\')|[^>\s]+))?)*)\s*(\/?)>/'
, '/<\/div[^>]*>$/');
preg_replace($patterns, array('','');, trim($htmlString));
Somewhat simpler.
If the 1 to 16 is significant and part of a greater range...
$match = preg_replace("/^<([^>]*)grid_(1[0-6]|[1-9])([^>\d]*)>(.+)<([^>]*)>$/s","$4",trim($str),-1,$hmany);
if($hmany){
echo "$match <br>";
}else{ echo "No match found! <br>"; }
If the 1 to 16 is the only possible range and therefore irrelevant...
$match = preg_replace("/^<([^>]*)>(.+)<([^>]*)>$/s","$2",trim($str),-1,$hmany);
if($hmany){
echo "$match <br>";
}else{ echo "No match found! <br>"; }
Regards.