使用 php 将字符串的第一个字符大写。然后用XMLWriter输出。

发布于 2024-12-12 11:51:55 字数 1517 浏览 2 评论 0原文

这可能非常简单,但我不知道最好的方法。我正在使用 php 从 mysqli 数据库中提取数据来创建 XML 文档。我下面的代码有效,但“标题”字段中的数据全部大写。我只需要第一个字符大写,其余小写。我知道我需要 ucwords 功能,但还没有骰子。标题字段包含多个全部大写的单词。

我需要在数据进入 XML 区域之前使用 ucwords 对其进行格式化。我更喜欢在 php 中执行此操作,而不是更新数据库中的数据。感谢您的帮助!

<?php

// Connect to the database
global $link;
$link = mysqli_connect("localhost", "root", "pass", "database");

// Verify the connection worked
if (!$link) {
    printf("Connection to database failed: %s\n", mysqli_connect_error());
    exit();
}

// Prepare the database query
   $stmt = mysqli_prepare($link, "SELECT * FROM table"); 

// Run the database query
   mysqli_stmt_execute($stmt);

// Bind the result columns to PHP variables
   mysqli_stmt_bind_result($stmt, $Name, $Title);    

// Create a new XML document in memory

$xw = new xmlWriter();
$xw->openURI('php://output');
$xw->openMemory();
$xw->startDocument('1.0');

// Start the outer data container
$xw->StartElement('rss');
$xw->WriteAttribute('version', '2.0');

// Fetch values
  while (mysqli_stmt_fetch($stmt)) {

{

$xw->startElement('item');

  // Write out the elements
    $xw->writeElement('Name', $Name);
    $xw->writeElement('Title', $Title);
    $xw->endElement();

}

// End container
$xw->endElement();

// End the document
$xw->endDocument();


//header('Content-Type: text/xml');
print $xw->outputMemory(true);

// Close the database statement
mysqli_stmt_close($stmt);

// Close the database connection
mysqli_close($link);
 }
?>

This is probably real simple, but I don't know best way to do this. I am pulling data from a mysqli database with php to create an XML document. My code below works but data in the Title field is all caps. I need just the first character capitalized, the rest lowercase. I know I need the ucwords function, but no dice yet. The Title field has more than one word in all caps.

I need the data formatted by ucwords before it goes into the XML area. I prefer to do this in the php instead of updating the data in the db. Thanks for the help!

<?php

// Connect to the database
global $link;
$link = mysqli_connect("localhost", "root", "pass", "database");

// Verify the connection worked
if (!$link) {
    printf("Connection to database failed: %s\n", mysqli_connect_error());
    exit();
}

// Prepare the database query
   $stmt = mysqli_prepare($link, "SELECT * FROM table"); 

// Run the database query
   mysqli_stmt_execute($stmt);

// Bind the result columns to PHP variables
   mysqli_stmt_bind_result($stmt, $Name, $Title);    

// Create a new XML document in memory

$xw = new xmlWriter();
$xw->openURI('php://output');
$xw->openMemory();
$xw->startDocument('1.0');

// Start the outer data container
$xw->StartElement('rss');
$xw->WriteAttribute('version', '2.0');

// Fetch values
  while (mysqli_stmt_fetch($stmt)) {

{

$xw->startElement('item');

  // Write out the elements
    $xw->writeElement('Name', $Name);
    $xw->writeElement('Title', $Title);
    $xw->endElement();

}

// End container
$xw->endElement();

// End the document
$xw->endDocument();


//header('Content-Type: text/xml');
print $xw->outputMemory(true);

// Close the database statement
mysqli_stmt_close($stmt);

// Close the database connection
mysqli_close($link);
 }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2024-12-19 11:51:55

http://php.net/manual/en/function.ucwords.php 中的相关部分

<?php
$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

对于您的查询,我将替换:

// Prepare the database query
$stmt = mysqli_prepare($link, "SELECT * FROM table"); 

// Run the database query
mysqli_stmt_execute($stmt);

// Bind the result columns to PHP variables
mysqli_stmt_bind_result($stmt, $Name, $Title);    

为:

$results = mysqli_query("SELECT * FROM table");

然后将 while 循环更改为:

foreach($results as $row) {
    $xw->startElement('item');
    $xw->writeElement('Name', ucwords(strtolower($row['name']));
    $xw->writeElement('Title', ucwords(strtolower($row['title']));
    $xw->endElement();
}

显然您需要对此进行修改,因为我不知道您的数据库架构。

更改 mysqli 内容的主要原因是,如果将来对数据库进行架构更改,则不能保证数据库列具有相同的顺序。

祝你好运!

The relevant section in http://php.net/manual/en/function.ucwords.php

<?php
$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!

$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

For your query, I'd replace:

// Prepare the database query
$stmt = mysqli_prepare($link, "SELECT * FROM table"); 

// Run the database query
mysqli_stmt_execute($stmt);

// Bind the result columns to PHP variables
mysqli_stmt_bind_result($stmt, $Name, $Title);    

With:

$results = mysqli_query("SELECT * FROM table");

Then change your while loop to:

foreach($results as $row) {
    $xw->startElement('item');
    $xw->writeElement('Name', ucwords(strtolower($row['name']));
    $xw->writeElement('Title', ucwords(strtolower($row['title']));
    $xw->endElement();
}

Obviously you need to tinker with this since I don't know your database schema.

The main reason for changing the mysqli stuff is that you aren't guaranteed to have the same ordering of the database columns if you make schema changes to the database in the future.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文