How do I split a Wordpress title at the – in PHP? - php

I am working on my Wordpress blog and its required to get the title of a post and split it at the "-". Thing is, its not working, because in the source its &ndash and when I look at the result on the website, its a "long minus" (–). Copying and pasting this long minus into some editor makes it a normal minus (-). I cant split at "-" nor at &ndash, but somehow it must be possible. When I created the article, I just typed "-" (minus), but somewhere it gets converted to – automatically.
Any ideas?
Thanks!

I think I found it. I remember that I have meet the similar problem that when I paste code in my post the quote mark transform to an em-quad one when display to readers.
I found that is in /wp-include/formatting.php line 56 (wordpress ver 3.3.1), it defined some characters need to replace
$static_characters = array_merge( array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'\'', ' (tm)'), $cockney );
$static_replacements = array_merge( array($em_dash, ' ' . $em_dash . ' ', $en_dash, ' ' . $en_dash . ' ', 'xn--', '…', $opening_quote, $closing_quote, ' ™'), $cockneyreplace );
and in line 85 it make an replacement
// This is not a tag, nor is the texturization disabled static strings
$curl = str_replace($static_characters, $static_replacements, $curl);

If you want to split a string at the "-" character, basically you must replace "-" with a space.
Try this:
$string_to_be_stripped = "my-word-test";
$chars = array('-');
$new_string = str_replace($chars, ' ', $string_to_be_stripped);
echo $new_string;
These lines splits the string at the "-". For example, if you have my-word-test, it will echo "my word test". I hope it helps.
For more information about the str_replace function click here.
If you want to do this in a WordPress style, try using filters. I suggest placing these lines in your functions.php file:
add_filter('the_title', function($title) {
$string_to_be_stripped = $title;
$chars = array('-');
$new_string = str_replace($chars, ' ', $string_to_be_stripped);
return $new_string;
})
Now, everytime you use the_title in a loop, the title will be escaped.

Related

Why is WordPress transforming my post's title differently than a regular string?

In WordPress, entitled My Town - Charleston, SC
In PHP 7.2, I would like to take this title and turn it into charleston,-sc
To do this, I'm using str_replace, strtolower, and trim.
$title = trim(str_replace('my town -', '', strtolower(get_the_title())));
$locationTag = str_replace(' ', '-', $title);
This works in regular PHP if I replace get_the_title with the string itself, but when I use the code on a WordPress template page, it does not replace my town - with a blank. Instead it only replaces spaces with dashes and makes everything lowercase.
Why is this happening? Does WordPress do some weird order of operations here to optimize stuff?
It's because for some reason get_the_title function converts '-' into '–'. I am not sure if you can see it clearly but the second dash is a bit wider.
I recreated the problem in my local environment and found this weird behavior.
var_dump( get_the_title() ); echo '<br>';
var_dump( $post->post_title ); echo '<br>';
var_dump( "My Town - Charleston, SC" ); echo '<br>';
You can see it clearly here in this screenshot-

Replacing the SPACE at first and last

I want to replace the first and last words and sentences .
I use this code.
$text = ' this is the test for string. ';
echo $text = str_replace(" ", "", $text);
when i have use replace code .
all space is deleted and repalsed.
any body can help me?!
i want get this:
this is the test for string.
You probably want the trim function here:
$text = ' this is the test for string. ';
echo '***' . trim($text) . '***';
***this is the test for string.***
Just to round out this answer, if you wanted to accomplish the same thing using a replacement, you could do a regex replace as follows:
$out = preg_replace("/^\s*|\s*$/", "", $text);
echo '***' . $out . '***';
***this is the test for string.***
This approach might a good starting point if you wanted to do a regex replacement with perhaps slightly different logic.

MediaWiki to Markdown conversion with PHP preg_replace

Since I discovered BoostNote (Open source note-taking app made for programmers), I decided to migrate from a local MediaWiki (on a NAS) to this new app.
But that also means migrating from MediaWiki syntax to Markdown!!
I tried several online conversion tools, but they didn't work very well. The most interesting one is Pandoc, but it also has a few problems (ex. I couldn't force it to use fenced blocks instead of indentation to delimit code blocks).
SOLUTION:
I ended up writing a tiny PHP script which represents a quick working solution (altough it may not be very elegant and polished ;) ). It is based on php preg_replace() function.
(The script will be available in the accepted answer below... if you have any suggestion, please post a comment)
Usage:
save MediaWiki markup in a txt file ('mediawiki.txt') in the same directory of the php script
run the script
a new txt file ('markdown.txt') will be created usgin markdown syntax
Enjoy!!
<?php
//Before running this script, fix manually all the mixed list (ordered + unordered). It's the only missing feature.
$string = file_get_contents("mediawiki.txt");
$pattern = array(
"/\*\*\*(?![^<]*>|[^<>]*<\/)/", //List: replace '***' with ' * '
"/\*\*(?![^<]*>|[^<>]*<\/)/", //List: replace '**' with ' * '
"/\*(?!\s+|[^<]*>|[^<>]*<\/)/", //List: replace '*' with '* '
"/\#\#\#(?![^<]*>|[^<>]*<\/)/", //List: replace '###' with ' 1. '
"/\#\#(?![^<]*>|[^<>]*<\/)/", //List: replace '##' with ' 1. '
"/\#(?![^<]*>|[^<>]*<\/)/", //List: replace '#' with '1. '
"/\'\'\'\'\'(.*?)\'\'\'\'\'(?![^<]*>|[^<>]*<\/)/", //Bold & Italic: replace "'''''(text)'''''" with "**_(text)_**"
"/\'\'\'([\s\S]*?)\'\'\'(?![^<]*>|[^<>]*<\/)/", //Bold: replace "'''(text)'''" with "**(text)**"
"/\'\'([\s\S]*?)\'\'(?![^<]*>|[^<>]*<\/)/", //Italic: replace "''(text)''" with "_(text)_"
"/\=\=\=\=([\s\S]*?)\=\=\=\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "====(text)====" with "#### (text)"
"/\=\=\=([\s\S]*?)\=\=\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "===(text)===" with "### (text)"
"/\=\=([\s\S]*?)\=\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "==(text)==" with "## (text)"
"/\=([\s\S]*?)\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "=(text)=" with "# (text)"
"/\{\{[\s\S]*?\|([\s\S]*?)\}\}(?![^<]*>|[^<>]*<\/)/", //Notes: replace "{{...|(text)}}" with "(text)"
"/\<pre[\s\S]*?\>([\s\S]*?)\<\/pre\>/", //Code block: replace "<pre ...>(code)</pre>" with "```java```" !!CHANGE THIS (php, javascript, etc)!!
"/\<code\>([\s\S]*?)\<\/code\>/", //Inline code: replace "<code>(code)</code>" with "`(code)`"
);
$replacement = array(
" * ",
" * ",
"* ",
" 1. ",
" 1. ",
"1. ",
"**_$1_**",
"**$1**",
"_$1_",
"#### $1",
"### $1",
"## $1",
"# $1",
"$1",
"```java
$1
```",
"`$1`",
);
$parsedString = preg_replace($pattern, $replacement, $string);
file_put_contents("markdown.txt", $parsedString);
?>

Preg Relplace only replace instances of text with a trailing space

I have a preg_replace question. I am using preg_replace to generate contextual links in text blocks using the following code:
$contextualLinkStr = 'mytext';
$content = 'My text string which includes MYTEXT in various cases such as Mytext and mytext. However it also includes image tags such as <img src="http://www.myurl.com/mytext-1.jpg">';
$content = preg_replace('/' . $contextualLinkStr . '/i', '\\0', $content);
The preg_replace is working well on the text and generating the relevant links while retaining case but it's also generating a link within the URL of the image tag. I was thinking If I simply added a trailing space to the expression in the preg_replace function it would fix it due to the fact that all text instances will have a trailing space whereas no image urls will, as follows:
$content = preg_replace('/' . $contextualLinkStr . '/i' . ' ', '\\0' . ' ', $content);
But this doesn't work. Can anybody tell me how I make the trailing space a condition of the match?
Thanks in advance.
Jason.
I've just worked it out guys. I was being daft. For reference the relevant code is:
$content = preg_replace('/' . $contextualLinkStr . ' /i', '\\0 ', $content);
Thanks.

Scan HTML for values with a special character before them

Say I have values on my page, like #100 #246, What I want to do is scan the page for values with a # before them and then alter them to put a hyperlink on it
$MooringNumbers = '#!' . $MooringNumbers . ' | ' . '#!' . $row1["Number"];
}
$viewedResult = '<tr><td>' .$Surname.'</td><td>'.$Title.'</td><td>'.$MooringNumbers . '</td><td>'.$Telephone.'</td><td>' . '[EDIT]</td>'.'<td>'. '[x]</td>'. '</tr>'; preg_replace('/#!(\d\d\d)/', '${1}', $viewedResult);
echo $viewedResult;
This is the broken code which doesnt work.
I second Xoc - use PHP manual. The method next to the one he pointed is preg-replace-callback
Just call:
preg_replace_callback(
'/#\d\d\d/',
create_function(
// single quotes are essential here,
// or alternative escape all $ as \$
'$matches',
'return strtolower($matches[0]);' //this you replace with what you want to fetch from database
)
EDIT:
Since you want to always perform the same replacement go with Xoc's preg-replace:
preg_replace('/#!(\d\d\d)/', '${1}', $your_input);
Note: I don't have PHP here, so I give no guarantee of this code not wiping your entire hard disk ;)
You can accomplish this by using regular expressions, see PHP's preg_replace function.
$text = 'Lorem ipsum #300 dolar amet #20';
preg_match_all('/(^|\s)#(\w+)/', $text, $matches);
// Perform you database magic here for each element in $matches[2]
var_dump($matches[2]);
// Fake query result
$query_result = array ( 300 => 'http://www.example1.com', 20 => 'http://www.example2.com');
foreach($query_result as $result_key => $result_value)
{
$text = str_replace('#'.$result_key, ''. $result_value . '', $text);
}
var_dump($text);

Categories