VID-1778 Script to remove categories with same names as video from file ...

...pages
I'd suggest renaming the loop variable to something like $titleText or the object variable something like $titleObj.  Having the same variable change meanings within a function can be confusing.