BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
PHP XML
PHP DOMText Function isWhitespaceInElementContent
PHP DOMNode Function replaceChild
PHP DOMNode Function lookupPrefix
PHP DOMNode Function lookupNamespaceURI
PHP DOMNode Function isSupported
PHP DOMNode Function isSameNode
PHP DOMNode Function insertBefore
PHP DOMNode Function hasChildNodes
PHP DOMNode Function hasAttributes
PHP DOMNode Function cloneNode
PHP DOMNodelist Function item
PHP DOMNamedNodeMap Function item
PHP DOMNamedNodeMap Function getNamedItemNS
PHP DOMNamedNodeMap Function getNamedItem
PHP DOMImplementation Function hasFeature
PHP DOMImplementation Function createDocumentType
PHP DOMImplementation Function createDocument
PHP DOMElement Function removeAttributeNS
PHP DOMElement Function removeAttributeNode
PHP DOMElement Function removeAttribute
 
 

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :218

 

DOMText Function splitText

PHP Version: (No version information might be only in CVS. I have used this function in PHP5)

Description

This function splits a text node into two at specified index.

Usage:

objectDOMText->splitText ( int offset)

Breaks this node into two nodes at the specified offset, keeping both in the tree as siblings. After being split, this node will contain all the content up to the offset. A new node of the same type, which contains all the content at and after the offset, is returned. If the original node had a parent node, the new node is inserted as the next sibling of the original node. When the offset is equal to the length of this node, the new node has no data.

PHP Code:

<?php

$xmlstr = <<<XML

<?xml version="1.0" encoding="UTF-8"?>

<company>

<employee>

<employeename id="myId1">test11 </employeename>

<employeeaddress>testAddress11</employeeaddress>

<SSN>11111</SSN>

<company>XYZ Corporation</company>

</employee>

<employee>

<employeename id="myId">test1</employeename>

<employeeaddress>testAddress1</employeeaddress>

<SSN>22222</SSN>

<company>ABC Corporation</company>

</employee>

</company>

XML;

//create a new DOMDocument

$dom = new domDocument;

//load the XML string into the DOM

$dom->loadXML($xmlstr);

//retrieve employeename elements

$employeenames = $dom->getElementsByTagName('employeename');

//get the first employeename

$employeename = $employeenames->item(0);

//create a text node with no white space

$textNode = $dom->createTextNode("Test1 Test2");

//split the text node from offset 5 so it would be Test2. After splitting textNode would contain Test1

$textNode1 = $textNode->splitText(5);

//append both $textNode1 into employeename element

$employeename->appendChild($textNode1);

echo "<HTML>";

echo "<Head>";

echo "<title>Example of DOMText->splitText</title>";

echo "<Head>";

echo "<body>";

echo "<PRE>";

echohtmlentities($dom->saveXML());

echo "</PRE>";

echo "</body>";

echo "</HTML>";

?>

The expected output would be:

<?xml version="1.0" encoding="UTF-8"?>
<company>
<employee>
<!-- Test2 prefixed by a space is appended here -->
      <employeename id="myId1">test11  Test2</employeename>
      <employeeaddress>testAddress11</employeeaddress>
      <SSN>11111</SSN>
      <company>XYZ Corporation</company>
</employee>
<employee>
      <employeename id="myId">test1</employeename>
      <employeeaddress>testAddress1</employeeaddress>
      <SSN>22222</SSN>
      <company>ABC Corporation</company>
</employee>
</company>



Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
online fax service
swimming pool contractor
teleconferencing services
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP