Page 1 of 1

splitting pHP strings

Posted: Thu Aug 19, 2004 3:27 am
by jocddy10
Hi,
I've recenty transformed some XML with XSL and PHP. as shown in the code below.
Now i need to split the $html string (which is the resultant HTML output of the XML file)
into single characters instead of printing it to screen. Inhave tried the ususal PHP methods for splitting a string, nothing seems to work. I have also tried putting the string into an array. Again, nothing works. Could anyone please help me?

Code: Select all

<?php

// Create an XSLT processor 
$xsltproc = xslt_create(); 

// Perform the transformation 
$html = xslt_process($xsltproc, 'results1.xml', 'results1.xsl');

// Detect errors 
if (!$html) die('XSLT processing error: '.xslt_error($xsltproc)); 

// Destroy the XSLT processor 
xslt_free($xsltproc); 
//print out the HTML
echo $html;

?>
Thanks

Jo

Posted: Thu Aug 19, 2004 5:35 am
by Buddha443556
You can use curly braces to access individual characters. Like this...

Code: Select all

<?php
// from the PHP Manuel

// Get the first character of a string
$str = 'This is a test.';
$first = $str{0};

// Get the third character of a string
$third = $str{2};

// Get the last character of a string.
$str = 'This is still a test.';
$last = $str{strlen($str)-1}; 

// Modify the last character of a string
$str = 'Look at the sea';
$str{strlen($str)-1} = 'e';
         
?>

Posted: Wed May 11, 2005 10:27 pm
by andresbezem
what about if I want to use the first 15 characters of a string, for example, the string

"Please do not forget your password as it has been encrypted in our database and we cannot retrieve it for you. However, should you forget your password you can request a new one which will be activated in the same way as this account.
"

I want only the first 15: "Please do not f"

And If I want that but the last word not to be cut, to continue until the following space, how can I do that?

Posted: Wed May 11, 2005 10:40 pm
by Skara

Code: Select all

$string = "Test String";
$string = substr($string,2,6);
// 'st Str'
you can also access strings as arrays. In other words, you can use either square or curly brackets.
$string[3] = $string{3}