Untested, but this should work
Create a PHP file
<?php
/**
* Fix HTML tags on the specified characters.
* @param string $text content
* @return \tidy
*/
function properHTML($text) {
if (!class_exists('tidy')) {
return $text;
}
// Tidy
$tidy = new tidy;
/**
* Change this
* @link http://tidy.sourceforge.net/docs/quickref.html
*/
$tidyConfig = array();
$tidy->parseString($text, $tidyConfig);
$tidy->cleanRepair();
if ($tidy->errorBuffer) {
$errorBuffer = "There are some errors!\n";
$errors = explode("\n", $tidy->errorBuffer);
foreach ($errors as $error) {
$errorBuffer .= $error . "\n";
}
echo $errorBuffer;
}
return $tidy;
}
///////////////// CHANGE THIS !!!!!!! ///////////////
$host = 'localhost';
$userhost = 'root';
$passhost = '123';
$mysql = mysqli_connect($host, $userhost, $passhost);
if (!$mysql) {
die('<strong>Database connection error: </strong>' . mysqli_error());
}
///////////////// CHANGE THIS !!!!!!! ///////////////
$db = 'change_with_your_db_name';
$table = 'modx_site_content';
$select = mysqli_select_db($mysql, $db);
if (!$select) {
die('<strong>Can not select database.</strong>');
}
$query = "SELECT id, description, content from " . $table;
$result = mysqli_query($mysql, $query);
if (!$result) {
die('<strong>Query is failed.</strong>');
}
///////////////// CHANGE THIS !!!!!!! ///////////////
// Add more doc's IDs if you want to skip them
$ignoreIds = array(1);
while ($l = mysqli_fetch_assoc($result)) {
if (empty($l['description']) &&
!in_array($l['id'], $ignoreIds)
) {
/**
* substr: strip by characters
*/
$description = substr($l['content'], 0, 150);
$description = properHTML($description);
mysqli_query($mysql, "UPDATE modx_site_content SET description=$description WHERE id=" . $l['id']);
}
}
mysqli_free_result($result);
mysqli_close($mysql);
What
properHTML does is to fix chopped of HTML tags when
substr does its work. Just make sure you have
PHP's tidy enabled. It will try to enclose missing HTML's pairing tags, eg <a href="...">bla bla [cut here].
If you don't like substr, you can explode('.', $l['content']) and return 1 or 2 sentences. It doesn't require that properHTML function.
while ($l = mysqli_fetch_assoc($result)) {
if (empty($l['description']) &&
!in_array($l['id'], $ignoreIds)
) {
/**
* strip by number of sentences
*/
$contents = explode('.', $l['content']);
$description = $contents[0] . '.' . $contents[1] . '.';
mysqli_query($mysql, "UPDATE modx_site_content SET description=$description WHERE id=" . $l['id']);
}
}
After that, delete again the file, and check the result.
[ed. note: goldsky last edited this post 13 years, 11 months ago.]