I have a problem with counting words in PHP, this is my code:
$word=substr(stripslashes(strip_tags($row['short_story'], '<a></a>')), 0,100 )."...";
$tpl->set( '{word}',$word);
on that code i just can show URL links in my result, i need to show complete style and HTML codes, so i changed to this:
$word = substr( stripslashes (strip_tags($row['short_story'], '<a><b><i><u><br></a><div></div>')), 0,400 )."...";
i changed this:
<a></a>
to:
<a><b><i><u><br></a><div></div>
But problem is, i have to set word limit over 400-500 if i want to show my result correct! because with 100 0r 200 word limit, HTML codes will be broken!
My question is, how i can use word count, but counting only numbers and letters? for example in this code:
<div style="color:red;">hello</div>
i need to count only hello word. is that possible?
Try the below code: it will work only when you know what tags are included in your string.
<?php
$tag_word = '<div style="color:red;">hello</div>';
// You can add any number of tags ex: strip_tags($tag_word, '<div><a><img><p>');
$word = strip_tags($tag_word, '<div>');
echo count($word);
?>
Related
I have never programmed in php before but I just found out that i need to edit the wp-admin page of a word press site and it MUST be done in php.
Actually what i need to do is change the color of a specific word.For example I need "cars" to be always on red. In jQuery that could be something like this:
$('p:contains("cars")').css('color', 'red');
Can anyone help me to write this in php please
You can use strpos and do something like that :
$text = "some text containing cars word";
if (strpos($text , 'cars') !== false) {
$style = 'style="color:red"';
}
else {
$style = "";
}
echo "<p ".$style.">".$text."</p>";
If the text contains "cars" word, strpos() will return the position of the word in the text. Else it will return false.
if you want to replace just words cars colored by red use preg_replace with flag g in pathern Regex to match all the words (cars) in the paragraph and replace them with red colored span. if you want change all paragraph use the code suggested by Titi
<?php
$paragraph = "<p>we all know cars faster than bikes!<p>";
echo preg_replace('/(cars)/g', '<span style="color:red;">$0</span>', $paragraph);
//<p>we all know <span style="color:red;">cars</span> faster than bikes!<p>
?>
It seems that you can use js script in the WordPress admin_enqueue_scripts.
Place your script into your plugin folder and then use it like that :
function my_enqueue($hook) {
wp_enqueue_script('my_custom_script', plugin_dir_url(__FILE__) . '/myscript.js');
}
add_action('admin_enqueue_scripts', 'my_enqueue');
This way, it calls a PHP function that calls a js file
Here is the official documentation about that : https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/
There are some posts about that at the bottom of the page.
I am trying to make "manner friendly" website. We use different declination dependent on gender and other factors. For example:
You did = robili
It did = robilo
She did = robila
Linguisticaly this is very simplified (and unlucky) example! I would like to change html text in php file where appropriate. For example
<? php
something
?>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^</div>
<? php something ?>
Now I would like to replace all occurences of different tokens ^characters|characters|characters^ and replace them by one of their internal values according to "gender".
It is easy in javascript on the client side, but you will see all this weird "tokenizing" before javascript replace it.
Here I do not know the elegant solution.
Or do you have better idea?
Thanks for advice.
You can add these scripts before and after the HTML:
<?php
// start output buffering
ob_start();
?>
<html>
<body>
html text of the page and somewhere is the word "robil"
<div>we tried to robil^i|o|a^, but also vital^si|sa|ste^, borko^mal|mala|malo^ </div>
</body>
</html>
<?php
$use = 1; // indicate which declination to use (0,1 or 2)
// get buffered html
$html = ob_get_contents();
ob_end_clean();
// match anything between '^' than's not a control chr or '^', min 5 and max 20 chrs.
if (preg_match_all('/\^[^[:cntrl:]\^]{3,20}\^/',$html,$matches))
{
// replace all
foreach (array_unique($matches[0]) as $match)
{
$choices = explode('|',trim($match,'^'));
$html = str_replace($match,$choices[$use],$html);
}
}
echo $html;
This returns:
html text of the page and somewhere is the word "robil" we tried to
robilo, but also vitalsa, borkomala
Hey I have on my database a text with 800 chracters when I echo it out it display in just one line getting too big and bad for read,
echo mysql_result(mysql_query("SELECT `message` FROM `pm` WHERE `pm_id` = '1'"));
How do I put or \n automatic on the end of every line because there is differents datas inserted in the database and do manualy for every one is hard.
edit:
I want to meke it like: one \n after 100 characters or something like that.
Use the wordwrap function: http://www.php.net/manual/en/function.wordwrap.php
echo wordwrap($data, 100, "\n"); // Or use "<br/>" in html
There also is a 4. parameter. If you data does not contain any spaces, wordwrap can not break between words. If you set the 4. parameter to true, wordwrap will cut no matter if within a word or not.
// cut after 100, no matter if inside a word.
echo wordwrap($data, 100, "\n", true);
put it in a div, and set div width. That will solve your problem. Like this:
#CSS
.sample{
width:50px;
}
and here's some sample html
<div class="sample">
#your results from database here
</div>
Good day everyone,
I'm very new with phpquery and this is my first post here at stackoverflow for a reason that i cant find the correct for syntax for the phpquery chaining. I know someone knows what i been looking for.
I only want to remove the a certain div inside a div.
<div id = "content">
<p>The text that i want to display</p>
<div class="node-links">Stuff i want to remove</div>
</content>
This few lines of codes works perfect
pq('div.node-links')->remove();
$text = pq('div#content');
print $text; //output: The text that i want to display
But when I tried
$text = pq('div#content')->removeClass('div.node-links'); //or
$text = pq('div#content')->remove('div.node-links');
//output: The text that i want to display (+) Stuff i want to remove
Can someone tell me why the second block of code is not working?
Thanks!
The first line of code will only work if your trying to remove the class from div.node-links, it won't remove the node.
If you are trying to remove the class you need to change it from:
$text = pq('div#content')->removeClass('div.node-links');
// to
$text = pq('div#content')->find('.node-links')->removeClass('node-links')->end();
which will output:
<div id="content">
<p>The text that i want to display</p>
<div>Stuff i want to remove</div>
</div>
As for the second line of code.. I'm not exactly sure why it is not working, it seems like your not selecting .node-links but I was able to get the desired results using these.
// $markup = file_get_contents('test.html');
// $doc = phpQuery::newDocumentHTML($markup);
$text = $doc->find('div#content')->children()->remove('.node-links')->end();
// or
$text = pq('div#content')->find('.node-links')->remove()->end();
// or
$text = pq('div#content > *')->remove('.node-links')->parent();
Hope that helps
Since remove() does not take any parameter, you can do:
$text = pq('div#content div.node-links')->remove();
I have the following table in mysql:-
id | bandname
1 | a perfect circle
2 | aerosmith
3 | b.b king
4 | cat stevens
I am fetching all of the results in a single query with:-
$result = mysql_query("SELECT id, bandname FROM bands ORDER BY bandname ASC");
On my page I have A-Z anchor links which bring up a new tab:-
<ul class="tabs-nav">
<li class="active">0-9</li>
<?php
// Print a-z link
for ($i=97; $i<=122; $i++) {
$curletter = chr($i);
echo '<li>'.$curletter.'</li>';
}
?>
</ul>
I'm having trouble getting the bands listed under their tab. Currently my code is:-
<div class="tabs-container">
<?php
// Print a-z tabs
for ($i=97; $i<=122; $i++) {
$curletter = chr($i);
?>
<div class="tab-content" id="<?php echo $curletter; ?>">
<?php
while($bands = mysql_fetch_array( $result )) {
$bandname = $bands['bandname'];
$bandid = $bands['id'];
$bandletter = strtolower(substr($bandname , 0 , 1));
}
if($curletter==$bandletter) {
echo '<a href="'.$bandid.'/" title="'.$bandname.'>'.$bandname.'</a>';
}
?>
</div>
<?php } ?>
</div>
I know this is incorrect because I'm calling the while loop inside the for loop and that just doesn't seem right to me - as every iteration of the A-Z tab creation process will have to run through the while loop.
If I will be dealing with say 5,000 bands what is the best approach for this? One sql result looped multiple times, one result with each band then held in an alphabetical array, or an ajax sql query whenever a user clicks on one of the anchor links?
None of this even starts to deal with the 0-9 tab which I think will be an issue itself given my current code. Any pointers really would be appreciated.
I have searched for an answer but not found something similiar to my question :)
Though I wouldn't recommend the method you've used at all, if you want to keep it as simple as you have then you need to extract all of the band names using the while before you enter the name looping foreach by putting them into a temporary array, or better still before any output takes place. This will allow you to use foreach() on this array instead of the while. You could also sort these into arrays by letter too, making it simpler to see which letters have bands that start with it
Some extra tips:
Use range() instead of the for($i = ... ) and chr() for the letters listings
Give your href values more than just the letter as the id (aesthetic but certainly better) such as letter-a letter-b and so on
If you're using 5000+ records you should be serving these on separate pages in my opinion with pagination rather than on one large page of bands
If these are not going to change very often, you'd be best using a caching system of some sort to keep database querying to a minimum
If you use the array by letter method I've given above, you can use the count() function to add the band number counts to the letter listings (and even hiding letters you have no bands for)