i have a paragraph in the database its like
$str ="this is a paragraph i show shortly and when i click on the view more it will show completely for that i am using the ajax and retrieve it "
i show this like
this is a paragraph i show shortly
php for show the first some word is
function chop_string($str, $x)// i called the function
{
$string = strip_tags(stripslashes($string));
return substr($string, 0, strpos(wordwrap($string, $x), "\n"));
}
and when user click on the view more it will display rest of it but the problem it that how to skip this this is a paragraph i show shortly and show the rest of it
i want to show the paragraph after the $x on click on view more
For truncating a string by character of word amount:
This SO question may be of help.
As would this one.
Here's some code that does it specifically by word count.
As far as showing more text on the click of a link goes, what I would suggest is loading the string from the database one time and format it's output. If your string is:
This is whole string pulled from my database.
Then the following code would be formatted like such:
HTML
<p class="truncate">This is the whole string <a class="showMore">Show more...</a><span> pulled from my database.</span></p>
CSS
p.truncate span { display: none; }
That way you can use Javascript (preferably through a library like jQuery which I've chosen for my code below) to hide or show more of your solution without having to make a second database request using AJAX. The following Javascript would do what you're requesting:
$("a.showMore").on("click", function() {
$(this).parent().find("span").contents().unwrap();
$(this).remove();
});
Here's a fiddle to play with!
I have made an example here: shaquin.tk/experiments/showmore.html.
You can view the source to see all of the code behind it. The PHP code is displayed on the page.
If you don't want to display the start string when Show more is clicked, replace the JavaScript function showMore with this:
function showMore() {
if(state == 0) {
state = 1;
document.getElementById('start').style.display = 'none';
document.getElementById('end').style.display = 'block';
document.getElementById('showmore').innerHTML = 'Show less';
document.getElementById('text-content').className = 'expanded';
document.getElementById('start').className = 'expanded';
} else {
state = 0;
document.getElementById('start').style.display = 'block';
document.getElementById('end').style.display = 'none';
document.getElementById('showmore').innerHTML = 'Show more';
document.getElementById('text-content').className = '';
document.getElementById('start').className = '';
}
}
Hope this helps.
Use this function :
function trim_text($string, $word_count)
{
$trimmed = "";
$string = preg_replace("/\040+/"," ", trim($string));
$stringc = explode(" ",$string);
//echo sizeof($stringc);
//echo " words <br /><br />";
if($word_count >= sizeof($stringc))
{
// nothing to do, our string is smaller than the limit.
return $string;
}
elseif($word_count < sizeof($stringc))
{
// trim the string to the word count
for($i=0;$i<$word_count;$i++)
{
$trimmed .= $stringc[$i]." ";
}
if(substr($trimmed, strlen(trim($trimmed))-1, 1) == '.')
return trim($trimmed).'..';
else
return trim($trimmed).'...';
}
}
$wordsBefore = 3;
$numOfWords = 7;
implode(' ', array_slice(explode(' ', $sentence), $wordsBefore, $wordsBefore+$numOfWords));
This will return the first 7 words of a sentence if you save it to a variable named sentence.
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 have the following PHP code:
function word_filter()
{
$tt_tijdelijke = '';
$tt_gestoffeerd = '';
$tt_gemeubileerd = '';
str_replace('tijdelijke', '<span class="incexc-single" tooltip="'.$tt_tijdelijke.'">tijdelijke</span>');
str_replace('gestoffeerd', '<span class="incexc-single" tooltip="'.$tt_gestoffeerd.'">gestoffeerd</span>');
str_replace('gemeubileerd', '<span class="incexc-single" tooltip="'.$tt_gemeubileerd.'">gemeubileerd</span>');
return true;
}
add_filter('wordfilter','word_filter');
I need this to run on every page in the website, replacing all the words with the same word, but inserted in a span. Doing so will allow a tooltip to appear when I mouse over the word.
The mouseover and tooltip already work, but I can't figure out how to get the replace to work on all pages.
How can I best call this function without causing too much page load?
I think you can use javascript split. the code will alert the text, but you can change that and use it for tooltip.
var txt = 'Word*tooltip';
alert(txt.split("*")[1]);
I am writing a simple php crawler that gets data from a website and inserts it into my database. I start with a predefined url. Then I go through the the contents of the page (from php's file_get_contents) and eventually use file_get_contents on links of that page. The url's I am getting from the links are fine when I echo them and then open them from my browser on their own. However, when I use file_get_contents and then echo the result, the page does not appear correctly because of errors related to dynamically created server-side data from the site. The echo'd page contents do not include the listed data from the server that I need, because it cannot find necessary resources for the site.
It appears relative paths in the echo'd webpage are not allowing the desired content to be generated.
Can anyone point me in the right direction here?
Any help is appreciated!
Here is some of my code so far:
function crawl_all($url)
{
$main_page = file_get_contents($url);
while(strpos($main_page, '"fl"') > 0)
{
$subj_start = strpos($main_page, '"fl"'); // get start of subject row
$main_page = substr($main_page, $subj_start); // cut off everything before subject row
$link_start = strpos($main_page, 'href') + 6; // get the start of the subject link
$main_page = substr($main_page, $link_start); // cut off everything before subject link
$link_end = strpos($main_page, '">') - 1; // get the end of the subject link
$link_length = $link_end + 1;
$link = substr($main_page, 0, $link_length); // get the subject link
crawl_courses('https://whatever.com' . $link);
}
}
/* Crawls all the courses for a subject. */
function crawl_courses($url)
{
$subj_page = file_get_contents($url);
echo $url; // website looks fine when in opened in browser
echo $subj_page; // when echo'd, the page does not contain most of the server-side generated data i need
while(strpos($subj_page, '<td><a href') > 0)
{
$course_start = strpos($subj_page, '<td><a href');
$subj_page = substr($subj_page, $course_start);
$link_start = strpos($subj_page, 'href') + 6;
$subj_page = substr($subj_page, $link_start);
$link_end = strpos($subj_page, '">') - 1;
$link_length = $link_end + 1;
$link = substr($subj_page, 0, $link_length);
//crawl_professors('https://whatever.com' . $link);
}
}
Try advance html dom parser. It is here....
http://sourceforge.net/projects/advancedhtmldom/
ive this html page ( PART CODE) with multi ( a href="https://twitter.com/$name)
I need to parse all $names and print in page
how i can do this ?
<td>Apr 01 2011<br><b>527
</b>
</td>
<td>
<a href="https://twitter.com/al_rasekhoon" class="twitter-follow-button" data-show count="false" data-lang="" data-width="60px" > al_rasekhoon</a>
</td>
</tr>
<tr class="rowc"><td colspan="11"></td></tr>
You need to loop over your $names array and print a correct a tag for every entry in that array. Like this:
<?php foreach($names as $name){ ?>
<?php echo $name ?>
<?php } ?>
Sounds like screen scraping, and you need to traverse the DOM for this. REs would be very unreliable.
DOMDocument may help you, but you might want to look into a library for screen scraping, such as BeautifulSoup (or some PHP equiv).
If I understand correctly you fetch a html page from somewhere and want to extract all linked twitter users? You can either parse the html code or do this with a bit of string splitting. This code is untested but should give you an idea:
$input = '(the html code)';
$links = explode('<a ', $input); //split input by start of link tags
for ($i = 0; $i < count($links); $i++) {
//cut off everything after the closing '>'
$links[$i] = explode('>', $links[$i], 2)[0]
//skip this link if it doesn't go to twitter.com
if (strpos($links[$i], 'href="twitter.com/') === False) { continue; }
//split by the 'href' attribute and keep everything after 'twitter.com'
$links[$i] = explode('href="twitter.com/', $links[$i], 2)[1]
//cut off everything after the " ending the href attribute
$links[$i] = explode('"', $links[$i], 2)[0]
//now $links[$i] should contain the twitter username
echo $links[$i]
}
Note: if there are other links to twitter on the page that are not the main page or an user, they will get printed too (e.g. if the page links to the twitter FAQ). You would need to filter them manually.
php sucks, let's do this in python!
input = '(the html code)'
links = [l.split(">", 1)[0] for l in input.split("<a ")}
twitter_links = [l for l in links if 'href="twitter.com/' in l]
twitter_hrefs = [l.split('href="twitter.com/', 1)[1] for l in twitter_links]
users = [l.split('"', 1)[0] for l in twitter_hrefs]
print '\n'.join(users)
i have a following feed from twitter and im making all links clickable and then i want that links which are inside a tag to be short to 30 chars, if its more then 30 chars then show ... after 30 chars
twitter feed
i need to start learning some real javascript from http://javascript.com/java/codes/snippet/search?q=javascript+limit+chars+leading some more text here, so dont remove this.
TO
i need to start learning some real javascript from http://javascript.com/java... some more text here, so dont remove this.
just need to know how can i truncate the inside of tag.
Edited
the link can be anywhere in whole text area.
To truncate a string, have a look at the trunc-prototype method for strings in my answer here. To acquire all links of a page use:
var linksHere = document.getElementsByTagName('a');
loop through your links and shorten the innerHTML of every link if the length is more than you want. Something like:
var i=-1,len = linksHere.length;
while (++i<len){
linksHere[i].innerHTML = linksHere[i].innerHTML.trunc(30);
}
Here's a handy truncating function I use.
// Examples
truncate('abcdefghijklmnopqrstuvwxyz'); // returns 'abcdefghijklmnopqrst...'
truncate('hello there', 15); // returns 'hello there'
truncate('hello there', 5, '...read more...'); // returns 'hello...read more...'
// Truncating method
function truncate(string, length, end)
{
if (typeof length == 'undefined')
{
length = 20;
}
if (typeof end == 'undefined')
{
end = '...';
}
if (string == null)
{
return '';
}
return string.substring(0, length-1)+(string.length > length ? end : '');
}