PHP: define a period or question mark at the end of a sentence

Problem: Some of the post names on my WordPress site are questions, i.e. The message header ends with a question mark. And I have mail requests that generate lists of recent messages. Therefore, I am trying to calculate a little php that will keep punctuation correct.

What is a good way to determine if the last title character is a question mark and not repeat the period? And if the message header is not a question, repeat the period.

This is what I am trying to use to get the title in the wordpress request and then determine if the title is a question, but it does not print a period.

There must be something simple, I'm wrong here:

<?php $mytitle = get_the_title(); echo $mytitle; ?>
<?php $question = substr($mytitle, -1); if (!$question = '?'): echo '.'; endif; ?>

Edit 3/03/10

Now it works:

<?php $mytitle = get_the_title(); echo $mytitle; ?>
<?php $question = substr($mytitle, -1); if ($question != '?') { echo '.'; } ?>
+3
3

:

<?php $mytitle = get_the_title(); echo $mytitle; ?>
<?php $question = substr($mytitle, -1); if ($question != '?') { echo '.'; } ?>
+2

, , , = , .

if (!$question = '?'): echo '.'; endif; 

:

if (!$question == '?'): echo '.'; endif; 

:

<?php $mytitle = get_the_title(); echo $mytitle; ?>
<?php $question = substr($mytitle, -1); if (!$question == '?'): echo '.'; endif; ?>

, .

+3

, . :

<?php

# add a period if it doesn't already end with punctuation.
function punc($s)
{
  if(preg_match("/[\.\?\,]$/", $s)) return $s;
  return $s . ".";
}

echo punc(get_the_title());
?>

punc() fancier . , , ?

0

Source: https://habr.com/ru/post/1735331/


All Articles