Question

I've imported data from SharePoint into a MySQL database and I need to extract and format some data out of a string while ignoring the numerical SharePoint IDs in between the delimiters. Example Name field with multiple entries as a string :

Smith, Bob;#5800;#Jones, Mark;#6067;#Brown, Alex

I want the output to look like this:

Smith, Bob</br>
Jones, Mark</br>
Brown, Alex

My current query works and returns the results. Current query:

<?php 
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
  <td><?php echo $row["Project Title"]; ?></td>
  <td><?php echo $row["Project Description"]; ?></td>
  <td><?php echo $row["Project Sponsor(s)"]; ?></td>
</tr>
<?php } ?>

The field containing this particular string is Project Sponsor(s).

How do I parse with these random IDs with the #; delimiter?

Help?

Was it helpful?

Solution

Just replace with <br />:

echo preg_replace('/;#?[^;]+;#?/', '<br />', $row['Project Sponsor(s)']);

OTHER TIPS

$ php -r 'print_r(explode(";#", "Smith, Bob;#5800;#Jones, Mark;#6067;#Brown, Alex"));'
Array
(
    [0] => Smith, Bob
    [1] => 5800
    [2] => Jones, Mark
    [3] => 6067
    [4] => Brown, Alex
)

You want 0, 2, and 4.

Try preg_split():

This regex might work or may need more tweaking:

$sponsors = preg_split("/;#[0-9]+;#/", $row["Project Sponsor(s)"]);

Now you can iterate over the list, format the names etc....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top