Removing Headers
Removing a Header from the HeaderSet is done by calling the HeaderSet's remove() or removeAll() methods.
To remove an existing header:
- Get the HeaderSet from the entity by via its getHeaders() method.
- Call the HeaderSet's remove() or removeAll() methods specifying the header you want to remove.
When calling remove() a single header will be removed. When calling removeAll() all headers with the given name will be removed. If no headers exist with the given name, no errors will occur.
Note:
It's valid for some headers to appear more than once in a message (e.g. the Received header).
For this reason remove() accepts an optional numerical index, starting from zero
to specify which header you want to check more specifically. For the same reason, removeAll()
exists to remove all headers that have the given name.
$headers = $message->getHeaders();
//Remove the Subject: header
$headers->remove('Subject');
//Remove all X-Foo headers
$headers->removeAll('X-Foo');
//Remove only the second X-Foo header
$headers->remove('X-Foo', 1);
Parent topic: Header Operations
Previous topic: Check if a Header Exists
Next topic: Modifying a Header's Content