I'm making a website and a PHPBB3 forum. I want the messages that are posted on the forum to show up on the main page of the website. I have gotten this concept down, except that the bbcode parsing is weird (phpbb3 adds a UID after each bbcode tag) and my bbcode parser does not include this. Since the UID is different for each post, I'm trying to get the PHP code to parse and remove this via MYSQL.
$query = #mysql_query("SELECT * FROM phpbb_posts where forum_id = 2;") or die (mysql_error());
$uid = $row['bbcode_uid'];
$content = $row['post_text'];
$content = str_replace($uid, '', $content);
$content = BBCode2html($content);
echo $content;
Is there a way I could replace the UID (bbcode looks like [u:1h77z1wc]UNDERLINED[/u:1h77z1wc] instead of [u]UNDERLINED[/u])?
You could replace it with a regex...
preg_replace('/\[((\w+)(:\w+))](.*?)\[\/\1]/s', '[$2]$4[/$2]', $str);
The following regular expression should do the trick:
preg_replace( '/\[(\/?[^:]+):[^\]]+\]/', '[$1]', $str );
Related
This question already has answers here:
Parse Wordpress like Shortcode
(7 answers)
Closed 4 years ago.
I'm using str_replace to replace a simple shortcode which works fine:
$content = "[old_shortcode]";
$old_shortcode = "[old_shortcode]";
$new_shortcode = "[new_shortcode]";
echo str_replace($old_shortcode, $new_shortcode, $content);
However I want to also replace attributes inside the shortcode without affecting any text content, for example change this:
[old_shortcode old_option_1="Text Content" old_option_2="Text Content"]
To this:
[new_shortcode new_option_1="Text Content" new_option_2="Text Content"]
Much appreciated if anyone could advise on how to do this.
To clarify, this question is not about parsing a shortcode (as it has been marked as a duplicated), it's about replacing one shortcode with another which the duplicate question linked to does not answer.
Edit:
I figured it out myself, however it's probably not a very elagant solution if anyone wants to suggest something better?
$pattern1 = '#\[shortcode(.*)attribute1="([^"]*)"(.*)\]#i';
$replace1 = '[shortcode$1attribute1_new="$2"$3]';
$pattern2 = '#\[shortcode(.*)attribute2="([^"]*)"(.*)\]#i';
$replace2 = '[shortcode$1attribute2_new="$2"$3]';
$pattern3 = '#\[shortcode(.*)(.*?)\[/shortcode\]#i';
$replace3 = '[new_shortcode$1[/new_shortcode]';
$content = '[shortcode attribute2="yes" attribute1="whatever"]Test[/shortcode]';
echo preg_replace(array($pattern1,$pattern2,$pattern3), array($replace1,$replace2,$replace3), $content);
Use preg_replace() instead that select only part of string you want using regex.
$newContent = preg_replace("/[a-zA-Z]+(_[^\s]+)/", "new$1", $content);
Check result in demo
Is there any way to check how many elements like headlines (h2,h3,...), paragraphs and links are in a WordPress post?
At the moment I'm using this code:
<?php
$content = get_the_content();
$count_h2s = explode('<h2>', $content);
$h2 = 0;
foreach ($count_h2s as $count_h2) {
$h2++;
}
echo $h2;
?>
It seems to work for the headlines. But if I'm using it to count <p>-tags I only get a count of 1. Even if there are more. I could imagine this is because these tags are not in the editor but headlines are?!
And maybe there is a more elegant way to count the elements than my code ;)
Loop is not necessary, use PHP function substr_count
$query = get_post(get_the_ID());
$content = apply_filters('the_content', $query->post_content);
$p_count = substr_count($content, '<p>');
echo $p_count ;
// Be aware, if there is a more-tag inside the post, this `<p>`-tag wouldn't count!
Should be easy to use it for other tags, such as ...
It looks like you're getting thrown off by the filter wordpress uses to automatically convert line breaks into <p> tags. The line breaks aren't in your editor because they are being added through the filter after the fact.
https://codex.wordpress.org/Function_Reference/wpautop
So, while you're seeing the <p> tags in the HTML source of your page, you want to search your get_the_content() for line breaks, instead, as these are what's being converted to <p> tags.
I am new to php. As a part of my course homework assignment , I am required to extract data from a website and using that data render a table.
P.S. : Using regex is not a good option but we are not allowed to use any library like DOM, jQuery etc.
Char set is UTF-8.
$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);
$patternform = '/<form(.*)<\/form>/sm';
preg_match_all($patternform ,$html,$matches);
Here regex works fine but when I apply the same regex for table tag, it return me empty array. Is there something to do with whitespaces in $html ?
What is wrong here?
The following code produces a good result:
$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);
$patternform = '/(<table.*<\/table>)/sm';
preg_match_all($patternform ,$html,$matches);
echo $matches[0][0];
Result:
I'm trying to do something similar to the way tags are added to questions on S/O using php/jquery.
$tagPart = $_POST['tagPart'];
$tagPart = strip_tags($tagPart,"");
$tagPart = trim($tagPart);
$tag_array = explode(',',$tagPart);
$lastTag = end($tag_array);
$part_query = "SELECT title FROM tag WHERE title LIKE '$lastTag%'";
$part_result = mysql_query($part_query) or die($lastTag);
while ($row = mysql_fetch_assoc($part_result)){
echo "<div id ='link' onclick = 'addText(\"".$row['title']."\");'>" . $row['title'] ."</div>";
}
This works well for adding the first tag..but doesn't show any results for the second tag after a comma is added...
so if i did PHP, JQuery for example...i would be able to add PHP but upon adding the , and the second tag the SQL query doesn't result in any results...i assume its probably matching the entire string and not just the last tag?
Try outputting your sql statement and check it for errors, extra spaces, etc.
$part_query = "SELECT title FROM tag WHERE title LIKE '$lastTag%'";
echo "sql statement: ".$part_query;
You trim $tagPart but maybe you need to trim $lastTag as well:
$lastTag = trim(end($tag_array));
Does $lastTag have a space in it? For example:
$tagPart = 'One, Two, Three';
$tag_array = explode(','$tagPart);
$lastTag = end($tag_array); //$lastTag = ' Three' -> notice the space!
I'm not a MySQL expert, so I'm not sure whether "LIKE" would automatically trim this off, but you may want to do $lastTag = trim(end($tag_array)). The fact that the first tag works would make sense if the space is the issue, because the first tag in the list doesn't have a preceding space.
I want help on this script I am making...
I want my website to be a wikipedia in itself... take for example I have a php website... I publish daily articles on it.
Suppose I publish 2 articles on Jenna Bush and Michael Jackson respectively
now I save into text/xml/database text and link
example
jenna bush, http://www.domain.com/jenna.html
michael jackson, http://www.domain.com/michael.html
or any which ways required like
<xml>
<item>
<text>jenna bush</text>
<link>http://www.domain.com/jenna.html</link>
</item>
... etc
</xml>
now what I want is the PHP script should automatically convert any jenna bush or any michael jackson linked to their respective links all over my website...
Any help is much appreciated...
Assuming that the text containing those words are in the database the best way to achieve something like that is using str_replace http://ie2.php.net/manual/en/function.str-replace.php
Right before the text is submitted to the database you run a function on it that looks for certain phrases and replaces them with other phrases.
Alternatively and probably a better approach is the same one that mediawiki (the software that wikipedia runs on uses), everytime you want to create a link to another article in a mediawiki you put [[ ]] around it, for example [[Michael Jackson]].
That way you have more control over what becomes a link.
Example: If you had an article on Prince the musician and one on Prince Charles and you wanted to link to Prince Charles, the first method might find Prince first and link to him, however if you use the mediawiki method you would write [[Prince Charles]] and it would know what to look for.
To do that I'd recommend preg_match http://www.php.net/manual/en/function.preg-match.php
It may be worth having a look at how mediawiki does the same thing, you can download it for free and it's written in php
I customized it and here is for everyone interested
function tags_autolink($text)
{
$text = " $text ";
$query_tags_autolink = "SELECT tag from tags";
$rs_tags_autolink = mysql_query($query_tags_autolink) or print "error getting tags";
while($row_tags_autolink = mysql_fetch_array($rs_tags_autolink))
{
$tag_name = trim($row_tags_autolink['tag']);
$tag_url = "http://www.domain.com/tag/".createLink(trim(htmlentities($tag_name)))."/";
$text = preg_replace("|(?!<[^<>]*?)(?<![?./&])\b($tag_name)\b(?!:)(?![^<>]*?>)|imsU","$1" , $text);
}
return trim( $text );
}
the create link function simply makes a string of "abcd is kk" like "abcd-is-kk" for a tag page ending ;)
cheers !
function auto_href($x)
{
$x = explode(' ', $x);
foreach ($x as $y)
{
if (substr($y, 0, 7) == 'http://')
$y = ''.$y.'';
$z[] = $y;
}
return implode($z, ' ');
}
function tags_autolink()
{
$conn = mysqli_connect("localhost", "root", "", "sample")
or die ("Could not connect to mysql because ".mysqli_error());
$text = 'You paragraph or text here';
$query_tags_autolink = "SELECT tag from tags";
$rs_tags_autolink = mysqli_query($conn,$query_tags_autolink) or print "error getting tags";
while($row_tags_autolink = mysqli_fetch_array($rs_tags_autolink))
{
$tag_name = trim($row_tags_autolink['tag']);
$trimedurl = str_replace(' ', '-',$tag_name);
$trimedurl=strtolower("$trimedurl");
$tag_url = "http://yourdomain/tag/$trimedurl";
$text = preg_replace("|(?!<[^<>]*?)(?<![?./&])\b($tag_name)\b(?!:)(?![^<>]*?>)|imsU","$1" , $text);
}
return trim($text);
}
echo tags_autolink() ;
Wikipedia's automatic hyperlinking code is in mediawiki:Parser.php, methods handleMagicLinks and makeFreeExternalLink.
The first searches for protocols, the latter removes stuff like trailing punctuation.